<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha256-CTSx/A06dm1B063156EVh15m6Y67pAjZZaQc89LLSrU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.css" integrity="sha256-vQkngPS8jiHHH0I6ABTZroZk8NPZ7b+MUReOFE9UsXQ=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"rjyblog.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.18.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":null},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="Android绘制流程">
<meta property="og:type" content="article">
<meta property="og:title" content="Android UI 绘制流程">
<meta property="og:url" content="https://rjyblog.gitee.io/posts/c09f2087.html">
<meta property="og:site_name" content="任建勇的博客">
<meta property="og:description" content="Android绘制流程">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://rjyblog.gitee.io/images/Android/RenderThread%E7%B1%BB%E5%9B%BE.drawio.svg">
<meta property="og:image" content="https://rjyblog.gitee.io/images/Android/Canvas%E7%B1%BB%E5%9B%BE.drawio.svg">
<meta property="og:image" content="https://rjyblog.gitee.io/images/Android/Surface%E6%9E%84%E5%BB%BA%E5%92%8C%E5%A1%AB%E5%85%85.drawio.svg">
<meta property="article:published_time" content="2023-09-26T01:45:29.000Z">
<meta property="article:modified_time" content="2024-01-06T08:11:20.359Z">
<meta property="article:author" content="Jason">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://rjyblog.gitee.io/images/Android/RenderThread%E7%B1%BB%E5%9B%BE.drawio.svg">


<link rel="canonical" href="https://rjyblog.gitee.io/posts/c09f2087.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://rjyblog.gitee.io/posts/c09f2087.html","path":"posts/c09f2087.html","title":"Android UI 绘制流程"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Android UI 绘制流程 | 任建勇的博客</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">任建勇的博客</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#draw-%E6%B5%81%E7%A8%8B"><span class="nav-text">draw() 流程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AF%E4%BB%B6%E6%B8%B2%E6%9F%93"><span class="nav-text">软件渲染</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A1%AC%E4%BB%B6%E6%B8%B2%E6%9F%93%EF%BC%88%E7%A1%AC%E4%BB%B6%E5%8A%A0%E9%80%9F%EF%BC%89"><span class="nav-text">硬件渲染（硬件加速）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#RenderThread%E5%A6%82%E4%BD%95%E5%90%AF%E5%8A%A8%E8%BF%90%E8%A1%8C"><span class="nav-text">RenderThread如何启动运行</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Surface%E7%9A%84%E7%94%9F%E6%88%90%E8%BF%87%E7%A8%8B"><span class="nav-text">Surface的生成过程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Surface%E5%88%86%E9%85%8D%E5%86%85%E5%AD%98"><span class="nav-text">Surface分配内存</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#BufferQueue%E7%94%9F%E4%BA%A7%E8%80%85%E5%92%8C%E6%B6%88%E8%B4%B9%E8%80%85"><span class="nav-text">BufferQueue生产者和消费者</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%99%84%E5%BD%95%E4%B8%80%EF%BC%9ACanvas-Java%E5%B1%82%E4%B8%8ENative%E6%BA%90%E7%A0%81%E5%AF%B9%E5%BA%94%E5%85%B3%E7%B3%BB"><span class="nav-text">附录一：Canvas Java层与Native源码对应关系</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B%EF%BC%9AdrawCircle"><span class="nav-text">示例：drawCircle</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BE%85%E5%8A%9E"><span class="nav-text">待办</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SurfaceView"><span class="nav-text">SurfaceView</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="nav-text">参考资料</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Jason"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">Jason</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">66</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://rjyblog.gitee.io/posts/c09f2087.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="Jason">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="任建勇的博客">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Android UI 绘制流程 | 任建勇的博客">
      <meta itemprop="description" content="Android绘制流程">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Android UI 绘制流程
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-09-26 09:45:29" itemprop="dateCreated datePublished" datetime="2023-09-26T09:45:29+08:00">2023-09-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-01-06 16:11:20" itemprop="dateModified" datetime="2024-01-06T16:11:20+08:00">2024-01-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
        </span>
    </span>

  
</div>

            <div class="post-description">Android绘制流程</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>Android View的整个绘制流程包含了3个阶段，分别是measure、layout、draw。measure用于计算View的宽高，结果放在<code>mMeasuredWidth</code>和<code>mMeasuredHeight</code>中；layout的作用是计算view及其子view的位置；draw的作用就是将View真正的绘制到显示屏上。</p>
<p>当activity首次启动，或者更新View(如TextView重新设置text)时，都会走到<code>ViewRootImpl.scheduleTraversals</code>来触发View的渲染流程。</p>
<pre class="mermaid">

zenuml
ViewRootImpl.scheduleTraversals {
    &quot;主线程消息循环插入同步屏障&quot;
    Choreographer.&quot;postCallback：mTraversalRunnable&quot; {
        scheduleFrameLocked {
            scheduleVsyncLocked {
                FrameDisplayEventReceiver.scheduleVsync()
            }
        }
    }
}
FrameDisplayEventReceiver.onVsync {
    Looper.&quot;发布一个同步消息&quot;
}
Looper.run {
    Choreographer.doFrame {
        &quot;doCallbacks(Choreographer.CALLBACK_INPUT, ...)&quot;
        &quot;doCallbacks(Choreographer.CALLBACK_ANIMATION,...)&quot;
        &quot;doCallbacks(Choreographer.CALLBACK_INSETS_ANIMATION,...)&quot;
        &quot;doCallbacks(Choreographer.CALLBACK_TRAVERSAL,...)&quot; {
            ViewRootImpl.&quot;mTraversalBarrier.run&quot; {
                ViewRootImpl.doTraversal {
                    &quot;删除同步屏障&quot;
                    performTraversals
                }
            }
        }
        &quot;doCallbacks(Choreographer.CALLBACK_COMMIT,...)&quot;
    }
}
</pre>

<pre class="mermaid">

zenuml
ViewRootImpl.performTraversals {
    measureHierarchy {
        performMeasure {
            DecorView.measure
        }
    }
    relayoutWindow {
        IWindowSession.relayout
        updateBlastSurfaceIfNeeded {
            new BLASTBufferQueue
            blastSurface &#x3D; BLASTBufferQueue.createSurface
            &quot;mSurface.transferFrom(blastSurface)&quot;
        }
        ThreadedRenderer.setSurfaceControl
        ThreadedRenderer.setBlastBufferQueue
    }
    ThreadedRenderer.initialize {
        &quot;HardwareRenderer.setSurface&quot;
    }
    ThreadedRenderer.allocateBuffers
    ThreadedRenderer.setup
    performMeasure {
        DecorView.measure
    }
    performLayout {
        DecorView.layout
    }
    performDraw {
        draw {
            ThreadedRenderer.draw
        }
    }
}
</pre>

<p>关于relayoutWindow的调用流程可参考<a href="/posts/90f60c72.html" title="Android WindowManager与Window">Android WindowManager与Window</a></p>
<p>参考：</p>
<ul>
<li><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/tyyj90/article/details/107134945">Android 源码 图形系统之请求布局</a></li>
<li><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/zuguorui/article/details/70160323">Android自定义view之measure、layout、draw三大流程</a></li>
</ul>
<h2 id="draw-流程"><a href="#draw-流程" class="headerlink" title="draw() 流程"></a>draw() 流程</h2><p>在自定义View中一般都会用到onDraw这个函数：</p>
<pre class="mermaid">

zenuml
ViewRootImpl.draw {
    if (isHardwareEnabled()) {
        ThreadedRenderer.draw
    } else {
        drawSoftware
    }
}
</pre>

<p>当开启硬件加速后，会使用ThreadedRenderer进行绘制，否则调用drawSoftware。</p>
<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="386px" preserveAspectRatio="none" style="width:537px;height:386px;background:#FFFFFF;" version="1.1" viewBox="0 0 537 386" width="537px" zoomAndPan="magnify"><defs/><g><!--class ViewRootImpl--><g id="elem_ViewRootImpl"><rect fill="#F1F1F1" height="48" id="ViewRootImpl" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="133" x="219" y="7"/><ellipse cx="234" cy="23" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M236.3438,18.6719 C235.4063,18.2344 234.8125,18.0938 233.9375,18.0938 C231.3125,18.0938 229.3125,20.1719 229.3125,22.8906 L229.3125,24.0156 C229.3125,26.5938 231.4219,28.4844 234.3125,28.4844 C235.5313,28.4844 236.6875,28.1875 237.4375,27.6406 C238.0156,27.2344 238.3438,26.7813 238.3438,26.3906 C238.3438,25.9375 237.9531,25.5469 237.4844,25.5469 C237.2656,25.5469 237.0625,25.625 236.875,25.8125 C236.4219,26.2969 236.4219,26.2969 236.2344,26.3906 C235.8125,26.6563 235.125,26.7813 234.3594,26.7813 C232.3125,26.7813 231.0156,25.6875 231.0156,23.9844 L231.0156,22.8906 C231.0156,21.1094 232.2656,19.7969 234,19.7969 C234.5781,19.7969 235.1875,19.9531 235.6563,20.2031 C236.1406,20.4844 236.3125,20.7031 236.4063,21.1094 C236.4688,21.5156 236.5,21.6406 236.6406,21.7656 C236.7813,21.9063 237.0156,22.0156 237.2344,22.0156 C237.5,22.0156 237.7656,21.875 237.9375,21.6563 C238.0469,21.5 238.0781,21.3125 238.0781,20.8906 L238.0781,19.4688 C238.0781,19.0313 238.0625,18.9063 237.9688,18.75 C237.8125,18.4844 237.5313,18.3438 237.2344,18.3438 C236.9375,18.3438 236.7344,18.4375 236.5156,18.75 L236.3438,18.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="101" x="248" y="27.8467">ViewRootImpl</text><line style="stroke:#181818;stroke-width:0.5;" x1="220" x2="351" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="220" x2="351" y1="47" y2="47"/></g><!--class Surface--><g id="elem_Surface"><rect fill="#F1F1F1" height="48" id="Surface" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="86" x="94.5" y="115"/><ellipse cx="109.5" cy="131" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M111.8438,126.6719 C110.9063,126.2344 110.3125,126.0938 109.4375,126.0938 C106.8125,126.0938 104.8125,128.1719 104.8125,130.8906 L104.8125,132.0156 C104.8125,134.5938 106.9219,136.4844 109.8125,136.4844 C111.0313,136.4844 112.1875,136.1875 112.9375,135.6406 C113.5156,135.2344 113.8438,134.7813 113.8438,134.3906 C113.8438,133.9375 113.4531,133.5469 112.9844,133.5469 C112.7656,133.5469 112.5625,133.625 112.375,133.8125 C111.9219,134.2969 111.9219,134.2969 111.7344,134.3906 C111.3125,134.6563 110.625,134.7813 109.8594,134.7813 C107.8125,134.7813 106.5156,133.6875 106.5156,131.9844 L106.5156,130.8906 C106.5156,129.1094 107.7656,127.7969 109.5,127.7969 C110.0781,127.7969 110.6875,127.9531 111.1563,128.2031 C111.6406,128.4844 111.8125,128.7031 111.9063,129.1094 C111.9688,129.5156 112,129.6406 112.1406,129.7656 C112.2813,129.9063 112.5156,130.0156 112.7344,130.0156 C113,130.0156 113.2656,129.875 113.4375,129.6563 C113.5469,129.5 113.5781,129.3125 113.5781,128.8906 L113.5781,127.4688 C113.5781,127.0313 113.5625,126.9063 113.4688,126.75 C113.3125,126.4844 113.0313,126.3438 112.7344,126.3438 C112.4375,126.3438 112.2344,126.4375 112.0156,126.75 L111.8438,126.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="54" x="123.5" y="135.8467">Surface</text><line style="stroke:#181818;stroke-width:0.5;" x1="95.5" x2="179.5" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="95.5" x2="179.5" y1="155" y2="155"/></g><!--class SurfaceControl--><g id="elem_SurfaceControl"><rect fill="#F1F1F1" height="48" id="SurfaceControl" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="140" x="215.5" y="115"/><ellipse cx="230.5" cy="131" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M232.8438,126.6719 C231.9063,126.2344 231.3125,126.0938 230.4375,126.0938 C227.8125,126.0938 225.8125,128.1719 225.8125,130.8906 L225.8125,132.0156 C225.8125,134.5938 227.9219,136.4844 230.8125,136.4844 C232.0313,136.4844 233.1875,136.1875 233.9375,135.6406 C234.5156,135.2344 234.8438,134.7813 234.8438,134.3906 C234.8438,133.9375 234.4531,133.5469 233.9844,133.5469 C233.7656,133.5469 233.5625,133.625 233.375,133.8125 C232.9219,134.2969 232.9219,134.2969 232.7344,134.3906 C232.3125,134.6563 231.625,134.7813 230.8594,134.7813 C228.8125,134.7813 227.5156,133.6875 227.5156,131.9844 L227.5156,130.8906 C227.5156,129.1094 228.7656,127.7969 230.5,127.7969 C231.0781,127.7969 231.6875,127.9531 232.1563,128.2031 C232.6406,128.4844 232.8125,128.7031 232.9063,129.1094 C232.9688,129.5156 233,129.6406 233.1406,129.7656 C233.2813,129.9063 233.5156,130.0156 233.7344,130.0156 C234,130.0156 234.2656,129.875 234.4375,129.6563 C234.5469,129.5 234.5781,129.3125 234.5781,128.8906 L234.5781,127.4688 C234.5781,127.0313 234.5625,126.9063 234.4688,126.75 C234.3125,126.4844 234.0313,126.3438 233.7344,126.3438 C233.4375,126.3438 233.2344,126.4375 233.0156,126.75 L232.8438,126.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="108" x="244.5" y="135.8467">SurfaceControl</text><line style="stroke:#181818;stroke-width:0.5;" x1="216.5" x2="354.5" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="216.5" x2="354.5" y1="155" y2="155"/></g><!--class SurfaceSession--><g id="elem_SurfaceSession"><rect fill="#F1F1F1" height="48" id="SurfaceSession" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="140" x="390.5" y="115"/><ellipse cx="405.5" cy="131" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M407.8438,126.6719 C406.9063,126.2344 406.3125,126.0938 405.4375,126.0938 C402.8125,126.0938 400.8125,128.1719 400.8125,130.8906 L400.8125,132.0156 C400.8125,134.5938 402.9219,136.4844 405.8125,136.4844 C407.0313,136.4844 408.1875,136.1875 408.9375,135.6406 C409.5156,135.2344 409.8438,134.7813 409.8438,134.3906 C409.8438,133.9375 409.4531,133.5469 408.9844,133.5469 C408.7656,133.5469 408.5625,133.625 408.375,133.8125 C407.9219,134.2969 407.9219,134.2969 407.7344,134.3906 C407.3125,134.6563 406.625,134.7813 405.8594,134.7813 C403.8125,134.7813 402.5156,133.6875 402.5156,131.9844 L402.5156,130.8906 C402.5156,129.1094 403.7656,127.7969 405.5,127.7969 C406.0781,127.7969 406.6875,127.9531 407.1563,128.2031 C407.6406,128.4844 407.8125,128.7031 407.9063,129.1094 C407.9688,129.5156 408,129.6406 408.1406,129.7656 C408.2813,129.9063 408.5156,130.0156 408.7344,130.0156 C409,130.0156 409.2656,129.875 409.4375,129.6563 C409.5469,129.5 409.5781,129.3125 409.5781,128.8906 L409.5781,127.4688 C409.5781,127.0313 409.5625,126.9063 409.4688,126.75 C409.3125,126.4844 409.0313,126.3438 408.7344,126.3438 C408.4375,126.3438 408.2344,126.4375 408.0156,126.75 L407.8438,126.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="108" x="419.5" y="135.8467">SurfaceSession</text><line style="stroke:#181818;stroke-width:0.5;" x1="391.5" x2="529.5" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="391.5" x2="529.5" y1="155" y2="155"/></g><!--class Canvas--><g id="elem_Canvas"><rect fill="#F1F1F1" height="48" id="Canvas" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="82" x="8.5" y="223"/><ellipse cx="23.5" cy="239" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M25.8438,234.6719 C24.9063,234.2344 24.3125,234.0938 23.4375,234.0938 C20.8125,234.0938 18.8125,236.1719 18.8125,238.8906 L18.8125,240.0156 C18.8125,242.5938 20.9219,244.4844 23.8125,244.4844 C25.0313,244.4844 26.1875,244.1875 26.9375,243.6406 C27.5156,243.2344 27.8438,242.7813 27.8438,242.3906 C27.8438,241.9375 27.4531,241.5469 26.9844,241.5469 C26.7656,241.5469 26.5625,241.625 26.375,241.8125 C25.9219,242.2969 25.9219,242.2969 25.7344,242.3906 C25.3125,242.6563 24.625,242.7813 23.8594,242.7813 C21.8125,242.7813 20.5156,241.6875 20.5156,239.9844 L20.5156,238.8906 C20.5156,237.1094 21.7656,235.7969 23.5,235.7969 C24.0781,235.7969 24.6875,235.9531 25.1563,236.2031 C25.6406,236.4844 25.8125,236.7031 25.9063,237.1094 C25.9688,237.5156 26,237.6406 26.1406,237.7656 C26.2813,237.9063 26.5156,238.0156 26.7344,238.0156 C27,238.0156 27.2656,237.875 27.4375,237.6563 C27.5469,237.5 27.5781,237.3125 27.5781,236.8906 L27.5781,235.4688 C27.5781,235.0313 27.5625,234.9063 27.4688,234.75 C27.3125,234.4844 27.0313,234.3438 26.7344,234.3438 C26.4375,234.3438 26.2344,234.4375 26.0156,234.75 L25.8438,234.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="50" x="37.5" y="243.8467">Canvas</text><line style="stroke:#181818;stroke-width:0.5;" x1="9.5" x2="89.5" y1="255" y2="255"/><line style="stroke:#181818;stroke-width:0.5;" x1="9.5" x2="89.5" y1="263" y2="263"/></g><!--class Bitmap--><g id="elem_Bitmap"><rect fill="#F1F1F1" height="48" id="Bitmap" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="85" x="7" y="331"/><ellipse cx="22" cy="347" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M24.3438,342.6719 C23.4063,342.2344 22.8125,342.0938 21.9375,342.0938 C19.3125,342.0938 17.3125,344.1719 17.3125,346.8906 L17.3125,348.0156 C17.3125,350.5938 19.4219,352.4844 22.3125,352.4844 C23.5313,352.4844 24.6875,352.1875 25.4375,351.6406 C26.0156,351.2344 26.3438,350.7813 26.3438,350.3906 C26.3438,349.9375 25.9531,349.5469 25.4844,349.5469 C25.2656,349.5469 25.0625,349.625 24.875,349.8125 C24.4219,350.2969 24.4219,350.2969 24.2344,350.3906 C23.8125,350.6563 23.125,350.7813 22.3594,350.7813 C20.3125,350.7813 19.0156,349.6875 19.0156,347.9844 L19.0156,346.8906 C19.0156,345.1094 20.2656,343.7969 22,343.7969 C22.5781,343.7969 23.1875,343.9531 23.6563,344.2031 C24.1406,344.4844 24.3125,344.7031 24.4063,345.1094 C24.4688,345.5156 24.5,345.6406 24.6406,345.7656 C24.7813,345.9063 25.0156,346.0156 25.2344,346.0156 C25.5,346.0156 25.7656,345.875 25.9375,345.6563 C26.0469,345.5 26.0781,345.3125 26.0781,344.8906 L26.0781,343.4688 C26.0781,343.0313 26.0625,342.9063 25.9688,342.75 C25.8125,342.4844 25.5313,342.3438 25.2344,342.3438 C24.9375,342.3438 24.7344,342.4375 24.5156,342.75 L24.3438,342.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="53" x="36" y="351.8467">Bitmap</text><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="91" y1="363" y2="363"/><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="91" y1="371" y2="371"/></g><!--class IGraphicBufferProducer--><g id="elem_IGraphicBufferProducer"><rect fill="#F1F1F1" height="48" id="IGraphicBufferProducer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="201" x="126" y="223"/><ellipse cx="141" cy="239" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M143.3438,234.6719 C142.4063,234.2344 141.8125,234.0938 140.9375,234.0938 C138.3125,234.0938 136.3125,236.1719 136.3125,238.8906 L136.3125,240.0156 C136.3125,242.5938 138.4219,244.4844 141.3125,244.4844 C142.5313,244.4844 143.6875,244.1875 144.4375,243.6406 C145.0156,243.2344 145.3438,242.7813 145.3438,242.3906 C145.3438,241.9375 144.9531,241.5469 144.4844,241.5469 C144.2656,241.5469 144.0625,241.625 143.875,241.8125 C143.4219,242.2969 143.4219,242.2969 143.2344,242.3906 C142.8125,242.6563 142.125,242.7813 141.3594,242.7813 C139.3125,242.7813 138.0156,241.6875 138.0156,239.9844 L138.0156,238.8906 C138.0156,237.1094 139.2656,235.7969 141,235.7969 C141.5781,235.7969 142.1875,235.9531 142.6563,236.2031 C143.1406,236.4844 143.3125,236.7031 143.4063,237.1094 C143.4688,237.5156 143.5,237.6406 143.6406,237.7656 C143.7813,237.9063 144.0156,238.0156 144.2344,238.0156 C144.5,238.0156 144.7656,237.875 144.9375,237.6563 C145.0469,237.5 145.0781,237.3125 145.0781,236.8906 L145.0781,235.4688 C145.0781,235.0313 145.0625,234.9063 144.9688,234.75 C144.8125,234.4844 144.5313,234.3438 144.2344,234.3438 C143.9375,234.3438 143.7344,234.4375 143.5156,234.75 L143.3438,234.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="169" x="155" y="243.8467">IGraphicBufferProducer</text><line style="stroke:#181818;stroke-width:0.5;" x1="127" x2="326" y1="255" y2="255"/><line style="stroke:#181818;stroke-width:0.5;" x1="127" x2="326" y1="263" y2="263"/></g><!--reverse link ViewRootImpl to Surface--><g id="link_ViewRootImpl_Surface"><path codeLine="1" d="M243.6302,61.9875 C218.8912,79.7065 194.594,97.108 169.806,114.862 " fill="none" id="ViewRootImpl-backto-Surface" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="253.386,55,246.179,55.2418,243.6302,61.9875,250.8373,61.7457,253.386,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link ViewRootImpl to SurfaceControl--><g id="link_ViewRootImpl_SurfaceControl"><path codeLine="2" d="M285.5,67 C285.5,84.658 285.5,96.941 285.5,114.678 " fill="none" id="ViewRootImpl-backto-SurfaceControl" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="285.5,55,281.5,61,285.5,67,289.5,61,285.5,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link ViewRootImpl to SurfaceSession--><g id="link_ViewRootImpl_SurfaceSession"><path codeLine="3" d="M333.7359,61.217 C362.9889,78.936 392.99,97.108 422.3,114.862 " fill="none" id="ViewRootImpl-backto-SurfaceSession" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="323.472,55,326.5316,61.5298,333.7359,61.217,330.6763,54.6872,323.472,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link Surface to Canvas--><g id="link_Surface_Canvas"><path codeLine="4" d="M110.7401,172.233 C96.081,189.891 83.5869,204.941 68.8613,222.678 " fill="none" id="Surface-backto-Canvas" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="118.405,163,111.4949,165.0615,110.7401,172.233,117.6502,170.1715,118.405,163" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link Canvas to Bitmap--><g id="link_Canvas_Bitmap"><path codeLine="5" d="M49.5,283 C49.5,300.6584 49.5,312.9408 49.5,330.6784 " fill="none" id="Canvas-backto-Bitmap" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="49.5,271,45.5,277,49.5,283,53.5,277,49.5,271" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link Surface to IGraphicBufferProducer--><g id="link_Surface_IGraphicBufferProducer"><path codeLine="7" d="M164.5283,172.1902 C179.3543,189.8482 192.026,204.941 206.919,222.678 " fill="none" id="Surface-backto-IGraphicBufferProducer" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="156.812,163,157.6067,170.1672,164.5283,172.1902,163.7335,165.023,156.812,163" style="stroke:#181818;stroke-width:1.0;"/></g><!--SRC=[2yjCBG_Apo_np2t8KT3IrLK8BYrAIqnEvGh38U6SdrTIb9-3KpuujRWuCp-F2ye5IpadvfKb5dD1AB28KsP9Rc815uegJ_UYn8ACp6IdqhIqrAA0eloKqkJK8W00]--></g></svg>

<p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://source.android.google.cn/docs/core/graphics/arch-sh?hl=zh-cn#surface">Surface</a>是一个接口，供生产方与消耗方交换缓冲区。用于显示 Surface 的 BufferQueue 通常配置为三重缓冲。缓冲区是按需分配的，因此，如果生产方足够缓慢地生成缓冲区（例如在 60 fps 的显示屏上以 30 fps 的速度缓冲），队列中可能只有两个分配的缓冲区。</p>
<h3 id="软件渲染"><a href="#软件渲染" class="headerlink" title="软件渲染"></a>软件渲染</h3><p>执行<code>ViewRootImpl#drawSoftware</code>方法。</p>
<ol>
<li>获取Canvas：canvas = mSurface.lockCanvas(dirty);</li>
<li>执行View#draw：mView.draw(canvas);</li>
<li>surface.unlockCanvasAndPost(canvas);</li>
</ol>
<p>执行<code>android-12.1.0_r27\frameworks\base\core\jni\android_view_Surface.cpp</code>的nativeUnlockCanvasAndPost方法。</p>
<p>Client连接的是BufferQueue的生成端，而消费端则是SurfaceFlinger。</p>
<pre class="mermaid">

zenuml
group Java {
    &lt;&lt;Java&gt;&gt; Surface
}
group cpp {
    Jni
    &lt;&lt;Cpp&gt;&gt; sfc as Surface
    &lt;&lt;Cpp&gt;&gt; cvs as Canvas
    &lt;&lt;Cpp&gt;&gt; IGraphicBufferProducer
}
Surface.lockCanvas {
    Jni.nativeLockCanvas {
        sfc.lock {
            dequeueBuffer {
                IGraphicBufferProducer.dequeueBuffer
            }
        }
        new cvs() {

        }
        cvs.setBuffer
    }
}
</pre>

<p>非硬件加速的场景，通过<code>lockCanvas()</code>来获取一个画布，锁定一个缓冲区用于在CPU上进行渲染。而<code>unlockCanvasAndPost()</code> 可解锁缓冲区并将其发送到合成器。</p>
<pre class="mermaid">

zenuml
group Java {
    &lt;&lt;Java&gt;&gt; Surface
}
group cpp {
    Jni
    &lt;&lt;Cpp&gt;&gt; sfc as Surface
    &lt;&lt;Cpp&gt;&gt; cvs as Canvas
}

Surface.unlockCanvasAndPost {
    unlockSwCanvasAndPost {
        Jni.nativeUnlockCanvasAndPost {
            cvs.setBuffer(nullptr)
            sfc.unlockAndPost {
                queueBuffer {
                    IGraphicBufferProducer.queueBuffer
                }
            }
        }
    }
}
</pre>

<h3 id="硬件渲染（硬件加速）"><a href="#硬件渲染（硬件加速）" class="headerlink" title="硬件渲染（硬件加速）"></a>硬件渲染（硬件加速）</h3><p>最终会通过RenderThread进行渲染<code>ViewRootImpl#draw</code>中调用<code>mAttachInfo.mThreadedRenderer.draw(mView, mAttachInfo, this);</code></p>
<p><img src="/images/Android/RenderThread%E7%B1%BB%E5%9B%BE.drawio.svg" alt="RenderThread类图"></p>
<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="623px" preserveAspectRatio="none" style="width:939px;height:623px;background:#FFFFFF;" version="1.1" viewBox="0 0 939 623" width="939px" zoomAndPan="magnify"><defs/><g><!--class RenderProxy--><g id="elem_RenderProxy"><rect fill="#F1F1F1" height="48" id="RenderProxy" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="124" x="479.5" y="7"/><ellipse cx="494.5" cy="23" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M496.8438,18.6719 C495.9063,18.2344 495.3125,18.0938 494.4375,18.0938 C491.8125,18.0938 489.8125,20.1719 489.8125,22.8906 L489.8125,24.0156 C489.8125,26.5938 491.9219,28.4844 494.8125,28.4844 C496.0313,28.4844 497.1875,28.1875 497.9375,27.6406 C498.5156,27.2344 498.8438,26.7813 498.8438,26.3906 C498.8438,25.9375 498.4531,25.5469 497.9844,25.5469 C497.7656,25.5469 497.5625,25.625 497.375,25.8125 C496.9219,26.2969 496.9219,26.2969 496.7344,26.3906 C496.3125,26.6563 495.625,26.7813 494.8594,26.7813 C492.8125,26.7813 491.5156,25.6875 491.5156,23.9844 L491.5156,22.8906 C491.5156,21.1094 492.7656,19.7969 494.5,19.7969 C495.0781,19.7969 495.6875,19.9531 496.1563,20.2031 C496.6406,20.4844 496.8125,20.7031 496.9063,21.1094 C496.9688,21.5156 497,21.6406 497.1406,21.7656 C497.2813,21.9063 497.5156,22.0156 497.7344,22.0156 C498,22.0156 498.2656,21.875 498.4375,21.6563 C498.5469,21.5 498.5781,21.3125 498.5781,20.8906 L498.5781,19.4688 C498.5781,19.0313 498.5625,18.9063 498.4688,18.75 C498.3125,18.4844 498.0313,18.3438 497.7344,18.3438 C497.4375,18.3438 497.2344,18.4375 497.0156,18.75 L496.8438,18.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="92" x="508.5" y="27.8467">RenderProxy</text><line style="stroke:#181818;stroke-width:0.5;" x1="480.5" x2="602.5" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="480.5" x2="602.5" y1="47" y2="47"/></g><!--class DrawFrameTask--><g id="elem_DrawFrameTask"><rect fill="#F1F1F1" height="48" id="DrawFrameTask" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="146" x="468.5" y="132"/><ellipse cx="483.5" cy="148" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M485.8438,143.6719 C484.9063,143.2344 484.3125,143.0938 483.4375,143.0938 C480.8125,143.0938 478.8125,145.1719 478.8125,147.8906 L478.8125,149.0156 C478.8125,151.5938 480.9219,153.4844 483.8125,153.4844 C485.0313,153.4844 486.1875,153.1875 486.9375,152.6406 C487.5156,152.2344 487.8438,151.7813 487.8438,151.3906 C487.8438,150.9375 487.4531,150.5469 486.9844,150.5469 C486.7656,150.5469 486.5625,150.625 486.375,150.8125 C485.9219,151.2969 485.9219,151.2969 485.7344,151.3906 C485.3125,151.6563 484.625,151.7813 483.8594,151.7813 C481.8125,151.7813 480.5156,150.6875 480.5156,148.9844 L480.5156,147.8906 C480.5156,146.1094 481.7656,144.7969 483.5,144.7969 C484.0781,144.7969 484.6875,144.9531 485.1563,145.2031 C485.6406,145.4844 485.8125,145.7031 485.9063,146.1094 C485.9688,146.5156 486,146.6406 486.1406,146.7656 C486.2813,146.9063 486.5156,147.0156 486.7344,147.0156 C487,147.0156 487.2656,146.875 487.4375,146.6563 C487.5469,146.5 487.5781,146.3125 487.5781,145.8906 L487.5781,144.4688 C487.5781,144.0313 487.5625,143.9063 487.4688,143.75 C487.3125,143.4844 487.0313,143.3438 486.7344,143.3438 C486.4375,143.3438 486.2344,143.4375 486.0156,143.75 L485.8438,143.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="114" x="497.5" y="152.8467">DrawFrameTask</text><line style="stroke:#181818;stroke-width:0.5;" x1="469.5" x2="613.5" y1="164" y2="164"/><line style="stroke:#181818;stroke-width:0.5;" x1="469.5" x2="613.5" y1="172" y2="172"/></g><!--class RenderThread--><g id="elem_RenderThread"><rect fill="#F1F1F1" height="48" id="RenderThread" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="134" x="275.5" y="241"/><ellipse cx="290.5" cy="257" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M292.8438,252.6719 C291.9063,252.2344 291.3125,252.0938 290.4375,252.0938 C287.8125,252.0938 285.8125,254.1719 285.8125,256.8906 L285.8125,258.0156 C285.8125,260.5938 287.9219,262.4844 290.8125,262.4844 C292.0313,262.4844 293.1875,262.1875 293.9375,261.6406 C294.5156,261.2344 294.8438,260.7813 294.8438,260.3906 C294.8438,259.9375 294.4531,259.5469 293.9844,259.5469 C293.7656,259.5469 293.5625,259.625 293.375,259.8125 C292.9219,260.2969 292.9219,260.2969 292.7344,260.3906 C292.3125,260.6563 291.625,260.7813 290.8594,260.7813 C288.8125,260.7813 287.5156,259.6875 287.5156,257.9844 L287.5156,256.8906 C287.5156,255.1094 288.7656,253.7969 290.5,253.7969 C291.0781,253.7969 291.6875,253.9531 292.1563,254.2031 C292.6406,254.4844 292.8125,254.7031 292.9063,255.1094 C292.9688,255.5156 293,255.6406 293.1406,255.7656 C293.2813,255.9063 293.5156,256.0156 293.7344,256.0156 C294,256.0156 294.2656,255.875 294.4375,255.6563 C294.5469,255.5 294.5781,255.3125 294.5781,254.8906 L294.5781,253.4688 C294.5781,253.0313 294.5625,252.9063 294.4688,252.75 C294.3125,252.4844 294.0313,252.3438 293.7344,252.3438 C293.4375,252.3438 293.2344,252.4375 293.0156,252.75 L292.8438,252.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="102" x="304.5" y="261.8467">RenderThread</text><line style="stroke:#181818;stroke-width:0.5;" x1="276.5" x2="408.5" y1="273" y2="273"/><line style="stroke:#181818;stroke-width:0.5;" x1="276.5" x2="408.5" y1="281" y2="281"/></g><!--class CanvasContext--><g id="elem_CanvasContext"><rect fill="#F1F1F1" height="48" id="CanvasContext" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="141" x="674" y="241"/><ellipse cx="689" cy="257" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M691.3438,252.6719 C690.4063,252.2344 689.8125,252.0938 688.9375,252.0938 C686.3125,252.0938 684.3125,254.1719 684.3125,256.8906 L684.3125,258.0156 C684.3125,260.5938 686.4219,262.4844 689.3125,262.4844 C690.5313,262.4844 691.6875,262.1875 692.4375,261.6406 C693.0156,261.2344 693.3438,260.7813 693.3438,260.3906 C693.3438,259.9375 692.9531,259.5469 692.4844,259.5469 C692.2656,259.5469 692.0625,259.625 691.875,259.8125 C691.4219,260.2969 691.4219,260.2969 691.2344,260.3906 C690.8125,260.6563 690.125,260.7813 689.3594,260.7813 C687.3125,260.7813 686.0156,259.6875 686.0156,257.9844 L686.0156,256.8906 C686.0156,255.1094 687.2656,253.7969 689,253.7969 C689.5781,253.7969 690.1875,253.9531 690.6563,254.2031 C691.1406,254.4844 691.3125,254.7031 691.4063,255.1094 C691.4688,255.5156 691.5,255.6406 691.6406,255.7656 C691.7813,255.9063 692.0156,256.0156 692.2344,256.0156 C692.5,256.0156 692.7656,255.875 692.9375,255.6563 C693.0469,255.5 693.0781,255.3125 693.0781,254.8906 L693.0781,253.4688 C693.0781,253.0313 693.0625,252.9063 692.9688,252.75 C692.8125,252.4844 692.5313,252.3438 692.2344,252.3438 C691.9375,252.3438 691.7344,252.4375 691.5156,252.75 L691.3438,252.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="109" x="703" y="261.8467">CanvasContext</text><line style="stroke:#181818;stroke-width:0.5;" x1="675" x2="814" y1="273" y2="273"/><line style="stroke:#181818;stroke-width:0.5;" x1="675" x2="814" y1="281" y2="281"/></g><!--class IRenderPipeline--><g id="elem_IRenderPipeline"><rect codeLine="5" fill="#F1F1F1" height="48" id="IRenderPipeline" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="144" x="672.5" y="350"/><ellipse cx="687.5" cy="366" fill="#B4A7E5" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M688.4531,362.7813 L690.1719,362.7813 C690.5625,362.7813 690.75,362.75 690.875,362.6719 C691.1406,362.5156 691.2813,362.2344 691.2813,361.9375 C691.2813,361.6719 691.1719,361.4063 690.9375,361.2344 C690.7656,361.125 690.625,361.0938 690.1719,361.0938 L685.0313,361.0938 C684.5938,361.0938 684.4688,361.1094 684.3125,361.2031 C684.0625,361.3594 683.9063,361.6563 683.9063,361.9375 C683.9063,362.2188 684.0469,362.4688 684.2656,362.6406 C684.4219,362.75 684.6094,362.7813 685.0313,362.7813 L686.75,362.7813 L686.75,369.2969 L685.0313,369.2969 C684.5938,369.2969 684.4688,369.3125 684.3125,369.4219 C684.0625,369.5781 683.9063,369.8594 683.9063,370.1563 C683.9063,370.4063 684.0469,370.6719 684.2656,370.8281 C684.4219,370.9531 684.625,371 685.0313,371 L690.1719,371 C690.9219,371 691.2813,370.7188 691.2813,370.1563 C691.2813,369.875 691.1719,369.625 690.9375,369.4531 C690.7656,369.3281 690.625,369.2969 690.1719,369.2969 L688.4531,369.2969 L688.4531,362.7813 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" font-style="italic" lengthAdjust="spacing" textLength="112" x="701.5" y="370.8467">IRenderPipeline</text><line style="stroke:#181818;stroke-width:0.5;" x1="673.5" x2="815.5" y1="382" y2="382"/><line style="stroke:#181818;stroke-width:0.5;" x1="673.5" x2="815.5" y1="390" y2="390"/></g><!--class SkiaPipeline--><g id="elem_SkiaPipeline"><rect fill="#F1F1F1" height="48" id="SkiaPipeline" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="120" x="684.5" y="459"/><ellipse cx="699.5" cy="475" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M701.8438,470.6719 C700.9063,470.2344 700.3125,470.0938 699.4375,470.0938 C696.8125,470.0938 694.8125,472.1719 694.8125,474.8906 L694.8125,476.0156 C694.8125,478.5938 696.9219,480.4844 699.8125,480.4844 C701.0313,480.4844 702.1875,480.1875 702.9375,479.6406 C703.5156,479.2344 703.8438,478.7813 703.8438,478.3906 C703.8438,477.9375 703.4531,477.5469 702.9844,477.5469 C702.7656,477.5469 702.5625,477.625 702.375,477.8125 C701.9219,478.2969 701.9219,478.2969 701.7344,478.3906 C701.3125,478.6563 700.625,478.7813 699.8594,478.7813 C697.8125,478.7813 696.5156,477.6875 696.5156,475.9844 L696.5156,474.8906 C696.5156,473.1094 697.7656,471.7969 699.5,471.7969 C700.0781,471.7969 700.6875,471.9531 701.1563,472.2031 C701.6406,472.4844 701.8125,472.7031 701.9063,473.1094 C701.9688,473.5156 702,473.6406 702.1406,473.7656 C702.2813,473.9063 702.5156,474.0156 702.7344,474.0156 C703,474.0156 703.2656,473.875 703.4375,473.6563 C703.5469,473.5 703.5781,473.3125 703.5781,472.8906 L703.5781,471.4688 C703.5781,471.0313 703.5625,470.9063 703.4688,470.75 C703.3125,470.4844 703.0313,470.3438 702.7344,470.3438 C702.4375,470.3438 702.2344,470.4375 702.0156,470.75 L701.8438,470.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="88" x="713.5" y="479.8467">SkiaPipeline</text><line style="stroke:#181818;stroke-width:0.5;" x1="685.5" x2="803.5" y1="491" y2="491"/><line style="stroke:#181818;stroke-width:0.5;" x1="685.5" x2="803.5" y1="499" y2="499"/></g><!--class SkiaOpenGLPipeline--><g id="elem_SkiaOpenGLPipeline"><rect fill="#F1F1F1" height="48" id="SkiaOpenGLPipeline" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="178" x="551.5" y="568"/><ellipse cx="566.5" cy="584" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M568.8438,579.6719 C567.9063,579.2344 567.3125,579.0938 566.4375,579.0938 C563.8125,579.0938 561.8125,581.1719 561.8125,583.8906 L561.8125,585.0156 C561.8125,587.5938 563.9219,589.4844 566.8125,589.4844 C568.0313,589.4844 569.1875,589.1875 569.9375,588.6406 C570.5156,588.2344 570.8438,587.7813 570.8438,587.3906 C570.8438,586.9375 570.4531,586.5469 569.9844,586.5469 C569.7656,586.5469 569.5625,586.625 569.375,586.8125 C568.9219,587.2969 568.9219,587.2969 568.7344,587.3906 C568.3125,587.6563 567.625,587.7813 566.8594,587.7813 C564.8125,587.7813 563.5156,586.6875 563.5156,584.9844 L563.5156,583.8906 C563.5156,582.1094 564.7656,580.7969 566.5,580.7969 C567.0781,580.7969 567.6875,580.9531 568.1563,581.2031 C568.6406,581.4844 568.8125,581.7031 568.9063,582.1094 C568.9688,582.5156 569,582.6406 569.1406,582.7656 C569.2813,582.9063 569.5156,583.0156 569.7344,583.0156 C570,583.0156 570.2656,582.875 570.4375,582.6563 C570.5469,582.5 570.5781,582.3125 570.5781,581.8906 L570.5781,580.4688 C570.5781,580.0313 570.5625,579.9063 570.4688,579.75 C570.3125,579.4844 570.0313,579.3438 569.7344,579.3438 C569.4375,579.3438 569.2344,579.4375 569.0156,579.75 L568.8438,579.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="146" x="580.5" y="588.8467">SkiaOpenGLPipeline</text><line style="stroke:#181818;stroke-width:0.5;" x1="552.5" x2="728.5" y1="600" y2="600"/><line style="stroke:#181818;stroke-width:0.5;" x1="552.5" x2="728.5" y1="608" y2="608"/></g><!--class SkiaVulkanPipeline--><g id="elem_SkiaVulkanPipeline"><rect fill="#F1F1F1" height="48" id="SkiaVulkanPipeline" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="168" x="764.5" y="568"/><ellipse cx="779.5" cy="584" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M781.8438,579.6719 C780.9063,579.2344 780.3125,579.0938 779.4375,579.0938 C776.8125,579.0938 774.8125,581.1719 774.8125,583.8906 L774.8125,585.0156 C774.8125,587.5938 776.9219,589.4844 779.8125,589.4844 C781.0313,589.4844 782.1875,589.1875 782.9375,588.6406 C783.5156,588.2344 783.8438,587.7813 783.8438,587.3906 C783.8438,586.9375 783.4531,586.5469 782.9844,586.5469 C782.7656,586.5469 782.5625,586.625 782.375,586.8125 C781.9219,587.2969 781.9219,587.2969 781.7344,587.3906 C781.3125,587.6563 780.625,587.7813 779.8594,587.7813 C777.8125,587.7813 776.5156,586.6875 776.5156,584.9844 L776.5156,583.8906 C776.5156,582.1094 777.7656,580.7969 779.5,580.7969 C780.0781,580.7969 780.6875,580.9531 781.1563,581.2031 C781.6406,581.4844 781.8125,581.7031 781.9063,582.1094 C781.9688,582.5156 782,582.6406 782.1406,582.7656 C782.2813,582.9063 782.5156,583.0156 782.7344,583.0156 C783,583.0156 783.2656,582.875 783.4375,582.6563 C783.5469,582.5 783.5781,582.3125 783.5781,581.8906 L783.5781,580.4688 C783.5781,580.0313 783.5625,579.9063 783.4688,579.75 C783.3125,579.4844 783.0313,579.3438 782.7344,579.3438 C782.4375,579.3438 782.2344,579.4375 782.0156,579.75 L781.8438,579.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="136" x="793.5" y="588.8467">SkiaVulkanPipeline</text><line style="stroke:#181818;stroke-width:0.5;" x1="765.5" x2="931.5" y1="600" y2="600"/><line style="stroke:#181818;stroke-width:0.5;" x1="765.5" x2="931.5" y1="608" y2="608"/></g><!--class DeferredLayerUpdater--><g id="elem_DeferredLayerUpdater"><rect fill="#F1F1F1" height="48" id="DeferredLayerUpdater" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="194" x="444.5" y="241"/><ellipse cx="459.5" cy="257" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M461.8438,252.6719 C460.9063,252.2344 460.3125,252.0938 459.4375,252.0938 C456.8125,252.0938 454.8125,254.1719 454.8125,256.8906 L454.8125,258.0156 C454.8125,260.5938 456.9219,262.4844 459.8125,262.4844 C461.0313,262.4844 462.1875,262.1875 462.9375,261.6406 C463.5156,261.2344 463.8438,260.7813 463.8438,260.3906 C463.8438,259.9375 463.4531,259.5469 462.9844,259.5469 C462.7656,259.5469 462.5625,259.625 462.375,259.8125 C461.9219,260.2969 461.9219,260.2969 461.7344,260.3906 C461.3125,260.6563 460.625,260.7813 459.8594,260.7813 C457.8125,260.7813 456.5156,259.6875 456.5156,257.9844 L456.5156,256.8906 C456.5156,255.1094 457.7656,253.7969 459.5,253.7969 C460.0781,253.7969 460.6875,253.9531 461.1563,254.2031 C461.6406,254.4844 461.8125,254.7031 461.9063,255.1094 C461.9688,255.5156 462,255.6406 462.1406,255.7656 C462.2813,255.9063 462.5156,256.0156 462.7344,256.0156 C463,256.0156 463.2656,255.875 463.4375,255.6563 C463.5469,255.5 463.5781,255.3125 463.5781,254.8906 L463.5781,253.4688 C463.5781,253.0313 463.5625,252.9063 463.4688,252.75 C463.3125,252.4844 463.0313,252.3438 462.7344,252.3438 C462.4375,252.3438 462.2344,252.4375 462.0156,252.75 L461.8438,252.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="162" x="473.5" y="261.8467">DeferredLayerUpdater</text><line style="stroke:#181818;stroke-width:0.5;" x1="445.5" x2="637.5" y1="273" y2="273"/><line style="stroke:#181818;stroke-width:0.5;" x1="445.5" x2="637.5" y1="281" y2="281"/></g><!--class ThreadBase--><g id="elem_ThreadBase"><rect fill="#F1F1F1" height="48" id="ThreadBase" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="116" x="140.5" y="132"/><ellipse cx="155.5" cy="148" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M157.8438,143.6719 C156.9063,143.2344 156.3125,143.0938 155.4375,143.0938 C152.8125,143.0938 150.8125,145.1719 150.8125,147.8906 L150.8125,149.0156 C150.8125,151.5938 152.9219,153.4844 155.8125,153.4844 C157.0313,153.4844 158.1875,153.1875 158.9375,152.6406 C159.5156,152.2344 159.8438,151.7813 159.8438,151.3906 C159.8438,150.9375 159.4531,150.5469 158.9844,150.5469 C158.7656,150.5469 158.5625,150.625 158.375,150.8125 C157.9219,151.2969 157.9219,151.2969 157.7344,151.3906 C157.3125,151.6563 156.625,151.7813 155.8594,151.7813 C153.8125,151.7813 152.5156,150.6875 152.5156,148.9844 L152.5156,147.8906 C152.5156,146.1094 153.7656,144.7969 155.5,144.7969 C156.0781,144.7969 156.6875,144.9531 157.1563,145.2031 C157.6406,145.4844 157.8125,145.7031 157.9063,146.1094 C157.9688,146.5156 158,146.6406 158.1406,146.7656 C158.2813,146.9063 158.5156,147.0156 158.7344,147.0156 C159,147.0156 159.2656,146.875 159.4375,146.6563 C159.5469,146.5 159.5781,146.3125 159.5781,145.8906 L159.5781,144.4688 C159.5781,144.0313 159.5625,143.9063 159.4688,143.75 C159.3125,143.4844 159.0313,143.3438 158.7344,143.3438 C158.4375,143.3438 158.2344,143.4375 158.0156,143.75 L157.8438,143.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="84" x="169.5" y="152.8467">ThreadBase</text><line style="stroke:#181818;stroke-width:0.5;" x1="141.5" x2="255.5" y1="164" y2="164"/><line style="stroke:#181818;stroke-width:0.5;" x1="141.5" x2="255.5" y1="172" y2="172"/></g><!--class WorkQueue--><g id="elem_WorkQueue"><rect fill="#F1F1F1" height="48" id="WorkQueue" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="115" x="7" y="241"/><ellipse cx="22" cy="257" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M24.3438,252.6719 C23.4063,252.2344 22.8125,252.0938 21.9375,252.0938 C19.3125,252.0938 17.3125,254.1719 17.3125,256.8906 L17.3125,258.0156 C17.3125,260.5938 19.4219,262.4844 22.3125,262.4844 C23.5313,262.4844 24.6875,262.1875 25.4375,261.6406 C26.0156,261.2344 26.3438,260.7813 26.3438,260.3906 C26.3438,259.9375 25.9531,259.5469 25.4844,259.5469 C25.2656,259.5469 25.0625,259.625 24.875,259.8125 C24.4219,260.2969 24.4219,260.2969 24.2344,260.3906 C23.8125,260.6563 23.125,260.7813 22.3594,260.7813 C20.3125,260.7813 19.0156,259.6875 19.0156,257.9844 L19.0156,256.8906 C19.0156,255.1094 20.2656,253.7969 22,253.7969 C22.5781,253.7969 23.1875,253.9531 23.6563,254.2031 C24.1406,254.4844 24.3125,254.7031 24.4063,255.1094 C24.4688,255.5156 24.5,255.6406 24.6406,255.7656 C24.7813,255.9063 25.0156,256.0156 25.2344,256.0156 C25.5,256.0156 25.7656,255.875 25.9375,255.6563 C26.0469,255.5 26.0781,255.3125 26.0781,254.8906 L26.0781,253.4688 C26.0781,253.0313 26.0625,252.9063 25.9688,252.75 C25.8125,252.4844 25.5313,252.3438 25.2344,252.3438 C24.9375,252.3438 24.7344,252.4375 24.5156,252.75 L24.3438,252.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="83" x="36" y="261.8467">WorkQueue</text><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="121" y1="273" y2="273"/><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="121" y1="281" y2="281"/></g><!--class Looper--><g id="elem_Looper"><rect fill="#F1F1F1" height="48" id="Looper" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="83" x="157" y="241"/><ellipse cx="172" cy="257" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M174.3438,252.6719 C173.4063,252.2344 172.8125,252.0938 171.9375,252.0938 C169.3125,252.0938 167.3125,254.1719 167.3125,256.8906 L167.3125,258.0156 C167.3125,260.5938 169.4219,262.4844 172.3125,262.4844 C173.5313,262.4844 174.6875,262.1875 175.4375,261.6406 C176.0156,261.2344 176.3438,260.7813 176.3438,260.3906 C176.3438,259.9375 175.9531,259.5469 175.4844,259.5469 C175.2656,259.5469 175.0625,259.625 174.875,259.8125 C174.4219,260.2969 174.4219,260.2969 174.2344,260.3906 C173.8125,260.6563 173.125,260.7813 172.3594,260.7813 C170.3125,260.7813 169.0156,259.6875 169.0156,257.9844 L169.0156,256.8906 C169.0156,255.1094 170.2656,253.7969 172,253.7969 C172.5781,253.7969 173.1875,253.9531 173.6563,254.2031 C174.1406,254.4844 174.3125,254.7031 174.4063,255.1094 C174.4688,255.5156 174.5,255.6406 174.6406,255.7656 C174.7813,255.9063 175.0156,256.0156 175.2344,256.0156 C175.5,256.0156 175.7656,255.875 175.9375,255.6563 C176.0469,255.5 176.0781,255.3125 176.0781,254.8906 L176.0781,253.4688 C176.0781,253.0313 176.0625,252.9063 175.9688,252.75 C175.8125,252.4844 175.5313,252.3438 175.2344,252.3438 C174.9375,252.3438 174.7344,252.4375 174.5156,252.75 L174.3438,252.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="51" x="186" y="261.8467">Looper</text><line style="stroke:#181818;stroke-width:0.5;" x1="158" x2="239" y1="273" y2="273"/><line style="stroke:#181818;stroke-width:0.5;" x1="158" x2="239" y1="281" y2="281"/></g><!--reverse link RenderProxy to DrawFrameTask--><g id="link_RenderProxy_DrawFrameTask"><path codeLine="1" d="M541.5,67.135 C541.5,89.126 541.5,110.012 541.5,131.968 " fill="none" id="RenderProxy-backto-DrawFrameTask" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="541.5,55.135,537.5,61.135,541.5,67.135,545.5,61.135,541.5,55.135" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="27" x="542.5" y="98.0669">new</text></g><!--reverse link RenderProxy to RenderThread--><g id="link_RenderProxy_RenderThread"><path codeLine="2" d="M484.1953,60.8389 C452.8293,78.2729 423.055,99.006 396.5,132 C369.953,164.984 354.778,212.799 347.626,240.916 " fill="none" id="RenderProxy-backto-RenderThread" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="494.684,55.009,487.4964,54.4277,484.1953,60.8389,491.3829,61.4202,494.684,55.009" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="52" x="397.5" y="160.5669">&#33719;&#21462;&#21333;&#20363;</text></g><!--reverse link RenderProxy to CanvasContext--><g id="link_RenderProxy_CanvasContext"><path codeLine="3" d="M571.1991,64.0489 C589.7851,84.1849 609.102,105.375 632.5,132 C665.631,169.702 703.198,214.387 725.315,240.901 " fill="none" id="RenderProxy-backto-CanvasContext" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="563.06,55.231,564.1903,62.353,571.1991,64.0489,570.0688,56.9269,563.06,55.231" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="27" x="672.5" y="160.5669">new</text></g><!--reverse link CanvasContext to IRenderPipeline--><g id="link_CanvasContext_IRenderPipeline"><path codeLine="4" d="M744.5,301.217 C744.5,319.165 744.5,331.892 744.5,349.828 " fill="none" id="CanvasContext-backto-IRenderPipeline" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="744.5,289.217,740.5,295.217,744.5,301.217,748.5,295.217,744.5,289.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link IRenderPipeline to SkiaPipeline--><g id="link_IRenderPipeline_SkiaPipeline"><path codeLine="6" d="M744.5,416.217 C744.5,434.165 744.5,440.892 744.5,458.828 " fill="none" id="IRenderPipeline-backto-SkiaPipeline" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="744.5,398.217,738.5,416.217,750.5,416.217,744.5,398.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link SkiaPipeline to SkiaOpenGLPipeline--><g id="link_SkiaPipeline_SkiaOpenGLPipeline"><path codeLine="7" d="M709.3882,520.1245 C691.9432,538.0724 680.456,549.8918 663.022,567.8279 " fill="none" id="SkiaPipeline-backto-SkiaOpenGLPipeline" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="721.934,507.217,705.0857,515.9425,713.6907,524.3064,721.934,507.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link SkiaPipeline to SkiaVulkanPipeline--><g id="link_SkiaPipeline_SkiaVulkanPipeline"><path codeLine="8" d="M779.6118,520.1245 C797.0568,538.0724 808.544,549.8918 825.978,567.8279 " fill="none" id="SkiaPipeline-backto-SkiaVulkanPipeline" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="767.066,507.217,775.3093,524.3064,783.9143,515.9425,767.066,507.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link DrawFrameTask to CanvasContext--><g id="link_DrawFrameTask_CanvasContext"><path codeLine="9" d="M596.1636,185.8125 C630.2136,203.7605 666.51,222.892 700.538,240.828 " fill="none" id="DrawFrameTask-backto-CanvasContext" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="585.548,180.217,588.9906,186.5533,596.1636,185.8125,592.721,179.4762,585.548,180.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link DrawFrameTask to RenderThread--><g id="link_DrawFrameTask_RenderThread"><path codeLine="10" d="M487.751,185.9 C454.372,203.848 418.953,222.892 385.596,240.828 " fill="none" id="DrawFrameTask-backto-RenderThread" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="498.32,180.217,491.1412,179.5355,487.751,185.9,494.9298,186.5815,498.32,180.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link DrawFrameTask to DeferredLayerUpdater--><g id="link_DrawFrameTask_DeferredLayerUpdater"><path codeLine="11" d="M541.5,192.217 C541.5,210.165 541.5,222.892 541.5,240.828 " fill="none" id="DrawFrameTask-backto-DeferredLayerUpdater" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="541.5,180.217,537.5,186.217,541.5,192.217,545.5,186.217,541.5,180.217" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="8" x="532.525" y="200.0311">1</text><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="8" x="532.475" y="229.796">n</text></g><!--reverse link ThreadBase to RenderThread--><g id="link_ThreadBase_RenderThread"><path codeLine="13" d="M244.1937,190.9531 C268.3467,208.9011 287.177,222.892 311.315,240.828 " fill="none" id="ThreadBase-backto-RenderThread" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="229.746,180.217,240.6151,195.769,247.7724,186.1371,229.746,180.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link ThreadBase to WorkQueue--><g id="link_ThreadBase_WorkQueue"><path codeLine="14" d="M160.0469,187.705 C137.5709,205.653 115.981,222.892 93.5193,240.828 " fill="none" id="ThreadBase-backto-WorkQueue" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="169.424,180.217,162.2395,180.8353,160.0469,187.705,167.2314,187.0867,169.424,180.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link ThreadBase to Looper--><g id="link_ThreadBase_Looper"><path codeLine="15" d="M198.5,192.217 C198.5,210.165 198.5,222.892 198.5,240.828 " fill="none" id="ThreadBase-backto-Looper" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="198.5,180.217,194.5,186.217,198.5,192.217,202.5,186.217,198.5,180.217" style="stroke:#181818;stroke-width:1.0;"/></g><!--SRC=[XP2x2i9044NxVCMIKaXXArOQ527WM-l1N354TyF4-03_GDHAVyNEhp7WPnWrn3m4ozbpxcLdUf0skCDginKbmn0cqxh1jC20V5TKXCHQw-MSppoOCSYEbCVn6fukuU5yl-qBRftaYlowaajibd5VzkrbjUAOus7kI6XEX7XA4nH8RXRLNHJlkmubHdhuuhO7sRJ-IADWxf9CfEmj2jlyuEdBvB1Urj-ABdLXOWfcs1PjmKFFfcXRJVl4QkJ7FygKfU2hPQpOxGO8a0UMKXxu2G00]--></g></svg>

<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="366px" preserveAspectRatio="none" style="width:974px;height:366px;background:#FFFFFF;" version="1.1" viewBox="0 0 974 366" width="974px" zoomAndPan="magnify"><defs/><g><rect fill="#FFFFFF" height="29.1328" style="stroke:#181818;stroke-width:1.0;" width="10" x="634.5" y="226.0938"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="79" x2="79" y1="36.2969" y2="331.4922"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="273.5" x2="273.5" y1="36.2969" y2="331.4922"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="400.5" x2="400.5" y1="36.2969" y2="331.4922"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="522.5" x2="522.5" y1="36.2969" y2="331.4922"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="639.5" x2="639.5" y1="36.2969" y2="331.4922"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="768.5" x2="768.5" y1="36.2969" y2="331.4922"/><line style="stroke:#181818;stroke-width:0.5;stroke-dasharray:5.0,5.0;" x1="904.5" x2="904.5" y1="36.2969" y2="331.4922"/><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="149" x="5" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="135" x="12" y="24.9951">ThreadedRenderer</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="149" x="5" y="330.4922"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="135" x="12" y="350.4873">ThreadedRenderer</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="106" x="220.5" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="92" x="227.5" y="24.9951">RenderProxy</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="106" x="220.5" y="330.4922"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="92" x="227.5" y="350.4873">RenderProxy</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="128" x="336.5" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="114" x="343.5" y="24.9951">DrawFrameTask</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="128" x="336.5" y="330.4922"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="114" x="343.5" y="350.4873">DrawFrameTask</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="97" x="474.5" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="83" x="481.5" y="24.9951">WorkQueue</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="97" x="474.5" y="330.4922"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="83" x="481.5" y="350.4873">WorkQueue</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="116" x="581.5" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="102" x="588.5" y="24.9951">RenderThread</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="116" x="581.5" y="330.4922"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="102" x="588.5" y="350.4873">RenderThread</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="123" x="707.5" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="109" x="714.5" y="24.9951">CanvasContext</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="123" x="707.5" y="330.4922"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="109" x="714.5" y="350.4873">CanvasContext</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="128" x="840.5" y="5"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="114" x="847.5" y="24.9951">IRenderPipeline</text><rect fill="#E2E2F0" height="30.2969" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="128" x="840.5" y="330.4922"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="114" x="847.5" y="350.4873">IRenderPipeline</text><rect fill="#FFFFFF" height="29.1328" style="stroke:#181818;stroke-width:1.0;" width="10" x="634.5" y="226.0938"/><ellipse cx="7.5" cy="66.6797" fill="none" rx="4" ry="4" style="stroke:#181818;stroke-width:1.5;"/><polygon fill="#181818" points="67.5,63.4297,77.5,67.4297,67.5,71.4297,71.5,67.4297" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="12" x2="73.5" y1="67.4297" y2="67.4297"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="32" x="15" y="62.3638">draw</text><polygon fill="#181818" points="261.5,92.5625,271.5,96.5625,261.5,100.5625,265.5,96.5625" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="79.5" x2="267.5" y1="96.5625" y2="96.5625"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="170" x="86.5" y="91.4966">jni&#35843;&#29992;syncAndDrawFrame</text><polygon fill="#181818" points="388.5,121.6953,398.5,125.6953,388.5,129.6953,392.5,125.6953" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="273.5" x2="394.5" y1="125.6953" y2="125.6953"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="73" x="280.5" y="120.6294">drawFrame</text><line style="stroke:#181818;stroke-width:1.0;" x1="400.5" x2="442.5" y1="154.8281" y2="154.8281"/><line style="stroke:#181818;stroke-width:1.0;" x1="442.5" x2="442.5" y1="154.8281" y2="167.8281"/><line style="stroke:#181818;stroke-width:1.0;" x1="401.5" x2="442.5" y1="167.8281" y2="167.8281"/><polygon fill="#181818" points="411.5,163.8281,401.5,167.8281,411.5,171.8281,407.5,167.8281" style="stroke:#181818;stroke-width:1.0;"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="82" x="407.5" y="149.7622">postAndWait</text><polygon fill="#181818" points="511,192.9609,521,196.9609,511,200.9609,515,196.9609" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="400.5" x2="517" y1="196.9609" y2="196.9609"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="28" x="407.5" y="191.895">post</text><polygon fill="#181818" points="534,222.0938,524,226.0938,534,230.0938,530,226.0938" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="528" x2="633.5" y1="226.0938" y2="226.0938"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="52" x="540" y="221.0278">&#33719;&#21462;&#20219;&#21153;</text><polygon fill="#181818" points="411.5,251.2266,401.5,255.2266,411.5,259.2266,407.5,255.2266" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="405.5" x2="638.5" y1="255.2266" y2="255.2266"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="21" x="417.5" y="250.1606">run</text><polygon fill="#181818" points="757,280.3594,767,284.3594,757,288.3594,761,284.3594" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="400.5" x2="763" y1="284.3594" y2="284.3594"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="32" x="407.5" y="279.2935">draw</text><polygon fill="#181818" points="892.5,309.4922,902.5,313.4922,892.5,317.4922,896.5,313.4922" style="stroke:#181818;stroke-width:1.0;"/><line style="stroke:#181818;stroke-width:1.0;" x1="769" x2="898.5" y1="313.4922" y2="313.4922"/><text fill="#000000" font-family="sans-serif" font-size="13" lengthAdjust="spacing" textLength="32" x="776" y="308.4263">draw</text><!--SRC=[YyxNjLC8oIXAJKn9JGbApKj9BKejKh1II2bABEV240UgXR03Yl8hAe7Ail8oNsneVZvbHN5bNh9ZNeeBK9jRKM9kAXUoEg0sk4H8Od4sr7o8GbGPB4eByejBW4Q79sQMO2eEpo_A3YnDBKs5AeJQ2t4vke8NVTkVzazxidltqww5N8d99PbbYIMf2iWwCBIZEwQeD8yh9HMxNWp7EIVcbIKME-VdbQHMbC025LKGgCeJ6bIP1QavcNcfK7K0]--></g></svg>

<h3 id="RenderThread如何启动运行"><a href="#RenderThread如何启动运行" class="headerlink" title="RenderThread如何启动运行"></a>RenderThread如何启动运行</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">RenderThread&amp; <span class="title">RenderThread::getInstance</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    [[clang::no_destroy]] <span class="type">static</span> sp&lt;RenderThread&gt; sInstance = []() &#123;</span><br><span class="line">        sp&lt;RenderThread&gt; thread = sp&lt;RenderThread&gt;::<span class="built_in">make</span>();</span><br><span class="line">        thread-&gt;<span class="built_in">start</span>(<span class="string">&quot;RenderThread&quot;</span>);  <span class="comment">//开始执行threadLoop</span></span><br><span class="line">        <span class="keyword">return</span> thread;</span><br><span class="line">    &#125;();</span><br><span class="line">    gHasRenderThreadInstance = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">return</span> *sInstance;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">RenderThread::threadLoop</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">setpriority</span>(PRIO_PROCESS, <span class="number">0</span>, PRIORITY_DISPLAY);</span><br><span class="line">    Looper::<span class="built_in">setForThread</span>(mLooper);</span><br><span class="line">    <span class="keyword">if</span> (gOnStartHook) &#123;</span><br><span class="line">        <span class="built_in">gOnStartHook</span>(<span class="string">&quot;RenderThread&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">initThreadLocals</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">true</span>) &#123; <span class="comment">//loop forever</span></span><br><span class="line">        <span class="built_in">waitForWork</span>();</span><br><span class="line">        <span class="built_in">processQueue</span>(); <span class="comment">//执行WorkQueue中的任务</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (mPendingRegistrationFrameCallbacks.<span class="built_in">size</span>() &amp;&amp; !mFrameCallbackTaskPending) &#123;</span><br><span class="line">            mVsyncSource-&gt;<span class="built_in">drainPendingEvents</span>();</span><br><span class="line">            mFrameCallbacks.<span class="built_in">insert</span>(mPendingRegistrationFrameCallbacks.<span class="built_in">begin</span>(),</span><br><span class="line">                                   mPendingRegistrationFrameCallbacks.<span class="built_in">end</span>());</span><br><span class="line">            mPendingRegistrationFrameCallbacks.<span class="built_in">clear</span>();</span><br><span class="line">            <span class="built_in">requestVsync</span>();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!mFrameCallbackTaskPending &amp;&amp; !mVsyncRequested &amp;&amp; mFrameCallbacks.<span class="built_in">size</span>()) &#123;</span><br><span class="line">            <span class="comment">// <span class="doctag">TODO:</span> Clean this up. This is working around an issue where a combination</span></span><br><span class="line">            <span class="comment">// of bad timing and slow drawing can result in dropping a stale vsync</span></span><br><span class="line">            <span class="comment">// on the floor (correct!) but fails to schedule to listen for the</span></span><br><span class="line">            <span class="comment">// next vsync (oops), so none of the callbacks are run.</span></span><br><span class="line">            <span class="built_in">requestVsync</span>();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>会生成一个RecordingCanvas来记录所有的绘制步骤：</p>
<p><img src="/images/Android/Canvas%E7%B1%BB%E5%9B%BE.drawio.svg" alt="Canvas类图"></p>
<p>在使用硬件加速时，onDraw的canvas实际上是RecordingCanvas。RecordingCanvas最终的产物是DisplayList，然后交给RenderThread去执行真正的绘制流程。</p>
<h2 id="Surface的生成过程"><a href="#Surface的生成过程" class="headerlink" title="Surface的生成过程"></a>Surface的生成过程</h2><p>ViewRootImpl中通过Surface的无参构造函数创建了一个空的Surface。在<code>ViewRootImpl.relayoutWindow</code>中会调用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="type">int</span> <span class="title function_">relayoutWindow</span><span class="params">(WindowManager.LayoutParams params, <span class="type">int</span> viewVisibility,</span></span><br><span class="line"><span class="params">        <span class="type">boolean</span> insetsPending)</span> <span class="keyword">throws</span> RemoteException &#123;</span><br><span class="line">    ......</span><br><span class="line">    <span class="keyword">if</span> (mSurfaceControl.isValid()) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!useBLAST()) &#123;</span><br><span class="line">            mSurface.copyFrom(mSurfaceControl);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            updateBlastSurfaceIfNeeded();</span><br><span class="line">        &#125;</span><br><span class="line">    ......</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">void</span> <span class="title function_">updateBlastSurfaceIfNeeded</span><span class="params">()</span> &#123;</span><br><span class="line">    ......</span><br><span class="line">    mBlastBufferQueue = <span class="keyword">new</span> <span class="title class_">BLASTBufferQueue</span>(mTag, mSurfaceControl,</span><br><span class="line">            mSurfaceSize.x, mSurfaceSize.y, mWindowAttributes.format);</span><br><span class="line">    mBlastBufferQueue.setTransactionHangCallback(sTransactionHangCallback);</span><br><span class="line">    <span class="comment">//BlastBufferQueue.createSurface在native层生成一个BBQSurface类返回给java层</span></span><br><span class="line">    <span class="type">Surface</span> <span class="variable">blastSurface</span> <span class="operator">=</span> mBlastBufferQueue.createSurface();</span><br><span class="line">    <span class="comment">//将native Surface对象迁移到mSurface对象中</span></span><br><span class="line">    mSurface.transferFrom(blastSurface);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>BlastBufferQueue.createSurface在native层生成一个BBQSurface对象返回给java层</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//c++</span></span><br><span class="line"><span class="function">sp&lt;Surface&gt; <span class="title">BLASTBufferQueue::getSurface</span><span class="params">(<span class="type">bool</span> includeSurfaceControlHandle)</span> </span>&#123;</span><br><span class="line">    std::unique_lock _lock&#123;mMutex&#125;;</span><br><span class="line">    sp&lt;IBinder&gt; scHandle = <span class="literal">nullptr</span>;</span><br><span class="line">    <span class="keyword">if</span> (includeSurfaceControlHandle &amp;&amp; mSurfaceControl) &#123;</span><br><span class="line">        scHandle = mSurfaceControl-&gt;<span class="built_in">getHandle</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">BBQSurface</span>(mProducer, <span class="literal">true</span>, scHandle, <span class="keyword">this</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>BlastBufferQueue.createSurface返回给java层的对象是在jni层调用了下面这个Surface构造函数，因此这个Surface就关联了native的Surface了：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Surface.java</span></span><br><span class="line"><span class="keyword">private</span> <span class="title function_">Surface</span><span class="params">(<span class="type">long</span> nativeObject)</span> &#123;</span><br><span class="line">    <span class="keyword">synchronized</span> (mLock) &#123;</span><br><span class="line">        setNativeObjectLocked(nativeObject);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="/images/Android/Surface%E6%9E%84%E5%BB%BA%E5%92%8C%E5%A1%AB%E5%85%85.drawio.svg" alt="Surface构建和填充"></p>
<p><code>CanvasContext</code>是在<code>RenderProxy</code>的构造函数中构造的。</p>
<h3 id="Surface分配内存"><a href="#Surface分配内存" class="headerlink" title="Surface分配内存"></a>Surface分配内存</h3><p>内存分配是在ViewRootImpl.performTraversals中执行<code>mAttachInfo.mThreadedRenderer.allocateBuffers();</code>类为surface申请内存。allocateBuffers在HardwareRenderer中实现，会调用native端的<code>RenderProxy.allocateBuffers</code>来发起内存申请。</p>
<pre class="mermaid">

zenuml
RenderProxy.allocateBuffers {
    CanvasContext.allocateBuffers {
        &#x2F;&#x2F;NATIVE_WINDOW_ALLOCATE_BUFFERS
        Surface.&quot;perform&quot; {
            Surface.allocateBuffers {
                BufferQueueProducer.allocateBuffers {
                    new GraphicBuffer {
                        initWithSize {
                            GraphicBufferAllocator.allocate
                        }
                    }
                }
            }
        }
    }
}
</pre>

<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="195px" preserveAspectRatio="none" style="width:784px;height:195px;background:#FFFFFF;" version="1.1" viewBox="0 0 784 195" width="784px" zoomAndPan="magnify"><defs/><g><!--class GraphicBufferAllocator--><g id="elem_GraphicBufferAllocator"><rect codeLine="1" fill="#F1F1F1" height="72.5625" id="GraphicBufferAllocator" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="224" x="175.5" y="7"/><ellipse cx="202.2" cy="27.1328" fill="#FF7700" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M205.9813,23.1016 C205.9813,22.6641 205.9656,22.5234 205.8719,22.3672 C205.7313,22.1328 205.45,21.9766 205.1531,21.9766 C204.8094,21.9766 204.6688,22.1172 204.4969,22.5391 C203.9031,22.1641 203.1531,21.9766 202.2781,21.9766 C200.1688,21.9766 198.5906,23.2734 198.5906,24.9922 C198.5906,25.9609 199.1219,26.8672 199.9813,27.3516 C200.5281,27.6641 201.075,27.8359 202.1844,28.0234 C203.3406,28.2266 203.6063,28.2891 203.9656,28.4766 C204.3406,28.6797 204.575,29.0234 204.575,29.3828 C204.575,30.1172 203.5906,30.6641 202.3406,30.6641 C201.2156,30.6641 200.1375,30.1797 199.9656,29.5703 C199.8406,29.1016 199.8406,29.1016 199.7156,28.9922 C199.5594,28.8359 199.3406,28.7422 199.1063,28.7422 C198.825,28.7422 198.575,28.8672 198.4031,29.0859 C198.2938,29.2578 198.2469,29.4453 198.2469,29.8672 L198.2469,30.9922 C198.2469,31.7578 198.5281,32.1172 199.1219,32.1172 C199.3875,32.1172 199.5125,32.0547 199.7938,31.7109 C200.6375,32.1484 201.5281,32.3672 202.45,32.3672 C204.7781,32.3672 206.3406,31.1797 206.3406,29.4141 C206.3406,28.5234 206.0281,27.8359 205.3406,27.2891 C204.8094,26.8672 204.1688,26.6172 202.6844,26.3516 C201.4188,26.1172 201.325,26.0859 200.9969,25.9141 C200.6219,25.7266 200.3563,25.3359 200.3563,24.9766 C200.3563,24.2422 201.2156,23.6797 202.2781,23.6797 C203.3406,23.6797 204.1531,24.1484 204.3094,24.8203 C204.4188,25.3672 204.4188,25.3672 204.5438,25.5078 C204.6844,25.6328 204.9188,25.7422 205.1531,25.7422 C205.4188,25.7422 205.6688,25.6016 205.8406,25.3828 C205.95,25.2109 205.9813,25.0703 205.9813,24.6016 L205.9813,23.1016 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="12" font-style="italic" lengthAdjust="spacing" textLength="71" x="266.3" y="23.1387">&#171;Singleton&#187;</text><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="166" x="218.8" y="38.9639">GraphicBufferAllocator</text><line style="stroke:#181818;stroke-width:0.5;" x1="176.5" x2="398.5" y1="47.2656" y2="47.2656"/><rect fill="none" height="6" style="stroke:#C82930;stroke-width:1.0;" width="6" x="183.5" y="57.9141"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="198" x="195.5" y="64.2607">GrallocAllocator mAllocator</text><line style="stroke:#181818;stroke-width:0.5;" x1="176.5" x2="398.5" y1="71.5625" y2="71.5625"/></g><!--class GraphicBuffer--><g id="elem_GraphicBuffer"><rect fill="#F1F1F1" height="48" id="GraphicBuffer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="133" x="7" y="19.5"/><ellipse cx="22" cy="35.5" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M24.3438,31.1719 C23.4063,30.7344 22.8125,30.5938 21.9375,30.5938 C19.3125,30.5938 17.3125,32.6719 17.3125,35.3906 L17.3125,36.5156 C17.3125,39.0938 19.4219,40.9844 22.3125,40.9844 C23.5313,40.9844 24.6875,40.6875 25.4375,40.1406 C26.0156,39.7344 26.3438,39.2813 26.3438,38.8906 C26.3438,38.4375 25.9531,38.0469 25.4844,38.0469 C25.2656,38.0469 25.0625,38.125 24.875,38.3125 C24.4219,38.7969 24.4219,38.7969 24.2344,38.8906 C23.8125,39.1563 23.125,39.2813 22.3594,39.2813 C20.3125,39.2813 19.0156,38.1875 19.0156,36.4844 L19.0156,35.3906 C19.0156,33.6094 20.2656,32.2969 22,32.2969 C22.5781,32.2969 23.1875,32.4531 23.6563,32.7031 C24.1406,32.9844 24.3125,33.2031 24.4063,33.6094 C24.4688,34.0156 24.5,34.1406 24.6406,34.2656 C24.7813,34.4063 25.0156,34.5156 25.2344,34.5156 C25.5,34.5156 25.7656,34.375 25.9375,34.1563 C26.0469,34 26.0781,33.8125 26.0781,33.3906 L26.0781,31.9688 C26.0781,31.5313 26.0625,31.4063 25.9688,31.25 C25.8125,30.9844 25.5313,30.8438 25.2344,30.8438 C24.9375,30.8438 24.7344,30.9375 24.5156,31.25 L24.3438,31.1719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="101" x="36" y="40.3467">GraphicBuffer</text><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="139" y1="51.5" y2="51.5"/><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="139" y1="59.5" y2="59.5"/></g><!--class GrallocAllocator--><g id="elem_GrallocAllocator"><rect fill="#F1F1F1" height="48" id="GrallocAllocator" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="147" x="435" y="19.5"/><ellipse cx="450" cy="35.5" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M452.3438,31.1719 C451.4063,30.7344 450.8125,30.5938 449.9375,30.5938 C447.3125,30.5938 445.3125,32.6719 445.3125,35.3906 L445.3125,36.5156 C445.3125,39.0938 447.4219,40.9844 450.3125,40.9844 C451.5313,40.9844 452.6875,40.6875 453.4375,40.1406 C454.0156,39.7344 454.3438,39.2813 454.3438,38.8906 C454.3438,38.4375 453.9531,38.0469 453.4844,38.0469 C453.2656,38.0469 453.0625,38.125 452.875,38.3125 C452.4219,38.7969 452.4219,38.7969 452.2344,38.8906 C451.8125,39.1563 451.125,39.2813 450.3594,39.2813 C448.3125,39.2813 447.0156,38.1875 447.0156,36.4844 L447.0156,35.3906 C447.0156,33.6094 448.2656,32.2969 450,32.2969 C450.5781,32.2969 451.1875,32.4531 451.6563,32.7031 C452.1406,32.9844 452.3125,33.2031 452.4063,33.6094 C452.4688,34.0156 452.5,34.1406 452.6406,34.2656 C452.7813,34.4063 453.0156,34.5156 453.2344,34.5156 C453.5,34.5156 453.7656,34.375 453.9375,34.1563 C454.0469,34 454.0781,33.8125 454.0781,33.3906 L454.0781,31.9688 C454.0781,31.5313 454.0625,31.4063 453.9688,31.25 C453.8125,30.9844 453.5313,30.8438 453.2344,30.8438 C452.9375,30.8438 452.7344,30.9375 452.5156,31.25 L452.3438,31.1719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="115" x="464" y="40.3467">GrallocAllocator</text><line style="stroke:#181818;stroke-width:0.5;" x1="436" x2="581" y1="51.5" y2="51.5"/><line style="stroke:#181818;stroke-width:0.5;" x1="436" x2="581" y1="59.5" y2="59.5"/></g><!--class Gralloc4Allocator--><g id="elem_Gralloc4Allocator"><rect fill="#F1F1F1" height="48" id="Gralloc4Allocator" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="155" x="240" y="140"/><ellipse cx="255" cy="156" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M257.3438,151.6719 C256.4063,151.2344 255.8125,151.0938 254.9375,151.0938 C252.3125,151.0938 250.3125,153.1719 250.3125,155.8906 L250.3125,157.0156 C250.3125,159.5938 252.4219,161.4844 255.3125,161.4844 C256.5313,161.4844 257.6875,161.1875 258.4375,160.6406 C259.0156,160.2344 259.3438,159.7813 259.3438,159.3906 C259.3438,158.9375 258.9531,158.5469 258.4844,158.5469 C258.2656,158.5469 258.0625,158.625 257.875,158.8125 C257.4219,159.2969 257.4219,159.2969 257.2344,159.3906 C256.8125,159.6563 256.125,159.7813 255.3594,159.7813 C253.3125,159.7813 252.0156,158.6875 252.0156,156.9844 L252.0156,155.8906 C252.0156,154.1094 253.2656,152.7969 255,152.7969 C255.5781,152.7969 256.1875,152.9531 256.6563,153.2031 C257.1406,153.4844 257.3125,153.7031 257.4063,154.1094 C257.4688,154.5156 257.5,154.6406 257.6406,154.7656 C257.7813,154.9063 258.0156,155.0156 258.2344,155.0156 C258.5,155.0156 258.7656,154.875 258.9375,154.6563 C259.0469,154.5 259.0781,154.3125 259.0781,153.8906 L259.0781,152.4688 C259.0781,152.0313 259.0625,151.9063 258.9688,151.75 C258.8125,151.4844 258.5313,151.3438 258.2344,151.3438 C257.9375,151.3438 257.7344,151.4375 257.5156,151.75 L257.3438,151.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="123" x="269" y="160.8467">Gralloc4Allocator</text><line style="stroke:#181818;stroke-width:0.5;" x1="241" x2="394" y1="172" y2="172"/><line style="stroke:#181818;stroke-width:0.5;" x1="241" x2="394" y1="180" y2="180"/></g><!--class Gralloc3Allocator--><g id="elem_Gralloc3Allocator"><rect fill="#F1F1F1" height="48" id="Gralloc3Allocator" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="156" x="430.5" y="140"/><ellipse cx="445.5" cy="156" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M447.8438,151.6719 C446.9063,151.2344 446.3125,151.0938 445.4375,151.0938 C442.8125,151.0938 440.8125,153.1719 440.8125,155.8906 L440.8125,157.0156 C440.8125,159.5938 442.9219,161.4844 445.8125,161.4844 C447.0313,161.4844 448.1875,161.1875 448.9375,160.6406 C449.5156,160.2344 449.8438,159.7813 449.8438,159.3906 C449.8438,158.9375 449.4531,158.5469 448.9844,158.5469 C448.7656,158.5469 448.5625,158.625 448.375,158.8125 C447.9219,159.2969 447.9219,159.2969 447.7344,159.3906 C447.3125,159.6563 446.625,159.7813 445.8594,159.7813 C443.8125,159.7813 442.5156,158.6875 442.5156,156.9844 L442.5156,155.8906 C442.5156,154.1094 443.7656,152.7969 445.5,152.7969 C446.0781,152.7969 446.6875,152.9531 447.1563,153.2031 C447.6406,153.4844 447.8125,153.7031 447.9063,154.1094 C447.9688,154.5156 448,154.6406 448.1406,154.7656 C448.2813,154.9063 448.5156,155.0156 448.7344,155.0156 C449,155.0156 449.2656,154.875 449.4375,154.6563 C449.5469,154.5 449.5781,154.3125 449.5781,153.8906 L449.5781,152.4688 C449.5781,152.0313 449.5625,151.9063 449.4688,151.75 C449.3125,151.4844 449.0313,151.3438 448.7344,151.3438 C448.4375,151.3438 448.2344,151.4375 448.0156,151.75 L447.8438,151.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="124" x="459.5" y="160.8467">Gralloc3Allocator</text><line style="stroke:#181818;stroke-width:0.5;" x1="431.5" x2="585.5" y1="172" y2="172"/><line style="stroke:#181818;stroke-width:0.5;" x1="431.5" x2="585.5" y1="180" y2="180"/></g><!--class Gralloc2Allocator--><g id="elem_Gralloc2Allocator"><rect fill="#F1F1F1" height="48" id="Gralloc2Allocator" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="155" x="622" y="140"/><ellipse cx="637" cy="156" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M639.3438,151.6719 C638.4063,151.2344 637.8125,151.0938 636.9375,151.0938 C634.3125,151.0938 632.3125,153.1719 632.3125,155.8906 L632.3125,157.0156 C632.3125,159.5938 634.4219,161.4844 637.3125,161.4844 C638.5313,161.4844 639.6875,161.1875 640.4375,160.6406 C641.0156,160.2344 641.3438,159.7813 641.3438,159.3906 C641.3438,158.9375 640.9531,158.5469 640.4844,158.5469 C640.2656,158.5469 640.0625,158.625 639.875,158.8125 C639.4219,159.2969 639.4219,159.2969 639.2344,159.3906 C638.8125,159.6563 638.125,159.7813 637.3594,159.7813 C635.3125,159.7813 634.0156,158.6875 634.0156,156.9844 L634.0156,155.8906 C634.0156,154.1094 635.2656,152.7969 637,152.7969 C637.5781,152.7969 638.1875,152.9531 638.6563,153.2031 C639.1406,153.4844 639.3125,153.7031 639.4063,154.1094 C639.4688,154.5156 639.5,154.6406 639.6406,154.7656 C639.7813,154.9063 640.0156,155.0156 640.2344,155.0156 C640.5,155.0156 640.7656,154.875 640.9375,154.6563 C641.0469,154.5 641.0781,154.3125 641.0781,153.8906 L641.0781,152.4688 C641.0781,152.0313 641.0625,151.9063 640.9688,151.75 C640.8125,151.4844 640.5313,151.3438 640.2344,151.3438 C639.9375,151.3438 639.7344,151.4375 639.5156,151.75 L639.3438,151.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="123" x="651" y="160.8467">Gralloc2Allocator</text><line style="stroke:#181818;stroke-width:0.5;" x1="623" x2="776" y1="172" y2="172"/><line style="stroke:#181818;stroke-width:0.5;" x1="623" x2="776" y1="180" y2="180"/></g><!--link GraphicBuffer to GraphicBufferAllocator--><g id="link_GraphicBuffer_GraphicBufferAllocator"><path codeLine="4" d="M140.375,43.5 C152.061,43.5 157.746,43.5 169.432,43.5 " fill="none" id="GraphicBuffer-to-GraphicBufferAllocator" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="175.432,43.5,166.432,39.5,170.432,43.5,166.432,47.5,175.432,43.5" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link GraphicBufferAllocator to GrallocAllocator--><g id="link_GraphicBufferAllocator_GrallocAllocator"><path codeLine="5" d="M411.727,43.5 C423.482,43.5 423.237,43.5 434.993,43.5 " fill="none" id="GraphicBufferAllocator-backto-GrallocAllocator" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="399.727,43.5,405.727,47.5,411.727,43.5,405.727,39.5,399.727,43.5" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link GrallocAllocator to Gralloc4Allocator--><g id="link_GrallocAllocator_Gralloc4Allocator"><path codeLine="6" d="M455.9246,77.1186 C422.2696,97.9989 388.312,119.0671 354.686,139.929 " fill="none" id="GrallocAllocator-backto-Gralloc4Allocator" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="471.22,67.629,452.7615,72.0201,459.0878,82.217,471.22,67.629" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link GrallocAllocator to Gralloc3Allocator--><g id="link_GrallocAllocator_Gralloc3Allocator"><path codeLine="7" d="M508.5,85.629 C508.5,106.5093 508.5,119.0671 508.5,139.929 " fill="none" id="GrallocAllocator-backto-Gralloc3Allocator" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="508.5,67.629,502.5,85.629,514.5,85.629,508.5,67.629" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link GrallocAllocator to Gralloc2Allocator--><g id="link_GrallocAllocator_Gralloc2Allocator"><path codeLine="8" d="M561.0754,77.1186 C594.7304,97.9989 628.688,119.0671 662.314,139.929 " fill="none" id="GrallocAllocator-backto-Gralloc2Allocator" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="545.78,67.629,557.9122,82.217,564.2385,72.0201,545.78,67.629" style="stroke:#181818;stroke-width:1.0;"/></g><!--SRC=[Iyv9B2vMS2zABCZ8J7OgJKjBBNBCoSbFJYp9BrAmiL7G2DPHTdCpDpSmq5G8pinBpqajoSzJiBDJgEPI009Ta5QGUeIcN3YJgvOBnM05NJiSDd7XS80MflbS61RQrEZ2bPaGgSwOI7L6S640]--></g></svg>

<p>实际分配内存是在SurfaceFlinger进程中，通过hal层的Gralloc来分配内存。可以使用<code>adb shell dumpsys SurfaceFlinger</code>命令查看内存分配情况，如下：</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">GraphicBufferAllocator buffers:</span><br><span class="line">    Handle |        Size |     W (Stride) x H | Layers |   Format |      Usage | Requestor</span><br><span class="line">0x716ff921acd0 | 10125.00 KiB | 1080 (1080) x 2400 |      1 |        1 | 0x    1b00 | FramebufferSurface</span><br><span class="line">0x716ff921b300 | 10125.00 KiB | 1080 (1080) x 2400 |      1 |        1 | 0x    1b00 | FramebufferSurface</span><br><span class="line">0x716ff9226430 |   82.69 KiB |  336 ( 336) x   63 |      1 |        1 | 0x     303 | RegionSamplingThread</span><br><span class="line">Total allocated by GraphicBufferAllocator (estimate): 20332.69 KB</span><br></pre></td></tr></table></figure>

<p>参考资料：</p>
<ul>
<li><a target="_blank" rel="noopener external nofollow noreferrer" href="https://juejin.cn/post/6844904025851166734">Android图形系统系统篇之Gralloc</a></li>
<li><a target="_blank" rel="noopener external nofollow noreferrer" href="https://zhuanlan.zhihu.com/p/253055674">Android图像显示系统（二）GraphicBuffer和Gralloc分析</a></li>
<li><a target="_blank" rel="noopener external nofollow noreferrer" href="https://juejin.cn/post/7033402333128032286">解读SurfaceFlinger中的BufferQueue</a></li>
</ul>
<h2 id="BufferQueue生产者和消费者"><a href="#BufferQueue生产者和消费者" class="headerlink" title="BufferQueue生产者和消费者"></a>BufferQueue生产者和消费者</h2><p>BufferQueue的生产者端连着app，消费者端连接的是SurfaceFligger。</p>
<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="710px" preserveAspectRatio="none" style="width:606px;height:710px;background:#FFFFFF;" version="1.1" viewBox="0 0 606 710" width="606px" zoomAndPan="magnify"><defs/><g><!--class IGraphicBufferProducer--><g id="elem_IGraphicBufferProducer"><rect codeLine="1" fill="#F1F1F1" height="48" id="IGraphicBufferProducer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="200" x="12" y="115"/><ellipse cx="27" cy="131" fill="#B4A7E5" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M27.9531,127.7813 L29.6719,127.7813 C30.0625,127.7813 30.25,127.75 30.375,127.6719 C30.6406,127.5156 30.7813,127.2344 30.7813,126.9375 C30.7813,126.6719 30.6719,126.4063 30.4375,126.2344 C30.2656,126.125 30.125,126.0938 29.6719,126.0938 L24.5313,126.0938 C24.0938,126.0938 23.9688,126.1094 23.8125,126.2031 C23.5625,126.3594 23.4063,126.6563 23.4063,126.9375 C23.4063,127.2188 23.5469,127.4688 23.7656,127.6406 C23.9219,127.75 24.1094,127.7813 24.5313,127.7813 L26.25,127.7813 L26.25,134.2969 L24.5313,134.2969 C24.0938,134.2969 23.9688,134.3125 23.8125,134.4219 C23.5625,134.5781 23.4063,134.8594 23.4063,135.1563 C23.4063,135.4063 23.5469,135.6719 23.7656,135.8281 C23.9219,135.9531 24.125,136 24.5313,136 L29.6719,136 C30.4219,136 30.7813,135.7188 30.7813,135.1563 C30.7813,134.875 30.6719,134.625 30.4375,134.4531 C30.2656,134.3281 30.125,134.2969 29.6719,134.2969 L27.9531,134.2969 L27.9531,127.7813 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" font-style="italic" lengthAdjust="spacing" textLength="168" x="41" y="135.8467">IGraphicBufferProducer</text><line style="stroke:#181818;stroke-width:0.5;" x1="13" x2="211" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="13" x2="211" y1="155" y2="155"/></g><!--class IGraphicBufferConsumer--><g id="elem_IGraphicBufferConsumer"><rect codeLine="2" fill="#F1F1F1" height="48" id="IGraphicBufferConsumer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="209" x="371.5" y="115"/><ellipse cx="386.5" cy="131" fill="#B4A7E5" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M387.4531,127.7813 L389.1719,127.7813 C389.5625,127.7813 389.75,127.75 389.875,127.6719 C390.1406,127.5156 390.2813,127.2344 390.2813,126.9375 C390.2813,126.6719 390.1719,126.4063 389.9375,126.2344 C389.7656,126.125 389.625,126.0938 389.1719,126.0938 L384.0313,126.0938 C383.5938,126.0938 383.4688,126.1094 383.3125,126.2031 C383.0625,126.3594 382.9063,126.6563 382.9063,126.9375 C382.9063,127.2188 383.0469,127.4688 383.2656,127.6406 C383.4219,127.75 383.6094,127.7813 384.0313,127.7813 L385.75,127.7813 L385.75,134.2969 L384.0313,134.2969 C383.5938,134.2969 383.4688,134.3125 383.3125,134.4219 C383.0625,134.5781 382.9063,134.8594 382.9063,135.1563 C382.9063,135.4063 383.0469,135.6719 383.2656,135.8281 C383.4219,135.9531 383.625,136 384.0313,136 L389.1719,136 C389.9219,136 390.2813,135.7188 390.2813,135.1563 C390.2813,134.875 390.1719,134.625 389.9375,134.4531 C389.7656,134.3281 389.625,134.2969 389.1719,134.2969 L387.4531,134.2969 L387.4531,127.7813 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" font-style="italic" lengthAdjust="spacing" textLength="177" x="400.5" y="135.8467">IGraphicBufferConsumer</text><line style="stroke:#181818;stroke-width:0.5;" x1="372.5" x2="579.5" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="372.5" x2="579.5" y1="155" y2="155"/></g><!--class BLASTBufferQueue--><g id="elem_BLASTBufferQueue"><rect fill="#F1F1F1" height="48" id="BLASTBufferQueue" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="168" x="210" y="7"/><ellipse cx="225" cy="23" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M227.3438,18.6719 C226.4063,18.2344 225.8125,18.0938 224.9375,18.0938 C222.3125,18.0938 220.3125,20.1719 220.3125,22.8906 L220.3125,24.0156 C220.3125,26.5938 222.4219,28.4844 225.3125,28.4844 C226.5313,28.4844 227.6875,28.1875 228.4375,27.6406 C229.0156,27.2344 229.3438,26.7813 229.3438,26.3906 C229.3438,25.9375 228.9531,25.5469 228.4844,25.5469 C228.2656,25.5469 228.0625,25.625 227.875,25.8125 C227.4219,26.2969 227.4219,26.2969 227.2344,26.3906 C226.8125,26.6563 226.125,26.7813 225.3594,26.7813 C223.3125,26.7813 222.0156,25.6875 222.0156,23.9844 L222.0156,22.8906 C222.0156,21.1094 223.2656,19.7969 225,19.7969 C225.5781,19.7969 226.1875,19.9531 226.6563,20.2031 C227.1406,20.4844 227.3125,20.7031 227.4063,21.1094 C227.4688,21.5156 227.5,21.6406 227.6406,21.7656 C227.7813,21.9063 228.0156,22.0156 228.2344,22.0156 C228.5,22.0156 228.7656,21.875 228.9375,21.6563 C229.0469,21.5 229.0781,21.3125 229.0781,20.8906 L229.0781,19.4688 C229.0781,19.0313 229.0625,18.9063 228.9688,18.75 C228.8125,18.4844 228.5313,18.3438 228.2344,18.3438 C227.9375,18.3438 227.7344,18.4375 227.5156,18.75 L227.3438,18.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="136" x="239" y="27.8467">BLASTBufferQueue</text><line style="stroke:#181818;stroke-width:0.5;" x1="211" x2="377" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="211" x2="377" y1="47" y2="47"/></g><!--class BufferQueueProducer--><g id="elem_BufferQueueProducer"><rect fill="#F1F1F1" height="48" id="BufferQueueProducer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="186" x="45" y="439"/><ellipse cx="60" cy="455" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M62.3438,450.6719 C61.4063,450.2344 60.8125,450.0938 59.9375,450.0938 C57.3125,450.0938 55.3125,452.1719 55.3125,454.8906 L55.3125,456.0156 C55.3125,458.5938 57.4219,460.4844 60.3125,460.4844 C61.5313,460.4844 62.6875,460.1875 63.4375,459.6406 C64.0156,459.2344 64.3438,458.7813 64.3438,458.3906 C64.3438,457.9375 63.9531,457.5469 63.4844,457.5469 C63.2656,457.5469 63.0625,457.625 62.875,457.8125 C62.4219,458.2969 62.4219,458.2969 62.2344,458.3906 C61.8125,458.6563 61.125,458.7813 60.3594,458.7813 C58.3125,458.7813 57.0156,457.6875 57.0156,455.9844 L57.0156,454.8906 C57.0156,453.1094 58.2656,451.7969 60,451.7969 C60.5781,451.7969 61.1875,451.9531 61.6563,452.2031 C62.1406,452.4844 62.3125,452.7031 62.4063,453.1094 C62.4688,453.5156 62.5,453.6406 62.6406,453.7656 C62.7813,453.9063 63.0156,454.0156 63.2344,454.0156 C63.5,454.0156 63.7656,453.875 63.9375,453.6563 C64.0469,453.5 64.0781,453.3125 64.0781,452.8906 L64.0781,451.4688 C64.0781,451.0313 64.0625,450.9063 63.9688,450.75 C63.8125,450.4844 63.5313,450.3438 63.2344,450.3438 C62.9375,450.3438 62.7344,450.4375 62.5156,450.75 L62.3438,450.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="154" x="74" y="459.8467">BufferQueueProducer</text><line style="stroke:#181818;stroke-width:0.5;" x1="46" x2="230" y1="471" y2="471"/><line style="stroke:#181818;stroke-width:0.5;" x1="46" x2="230" y1="479" y2="479"/></g><!--class BBQBufferQueueProducer--><g id="elem_BBQBufferQueueProducer"><rect fill="#F1F1F1" height="48" id="BBQBufferQueueProducer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="217" x="24.5" y="547"/><ellipse cx="39.5" cy="563" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M41.8438,558.6719 C40.9063,558.2344 40.3125,558.0938 39.4375,558.0938 C36.8125,558.0938 34.8125,560.1719 34.8125,562.8906 L34.8125,564.0156 C34.8125,566.5938 36.9219,568.4844 39.8125,568.4844 C41.0313,568.4844 42.1875,568.1875 42.9375,567.6406 C43.5156,567.2344 43.8438,566.7813 43.8438,566.3906 C43.8438,565.9375 43.4531,565.5469 42.9844,565.5469 C42.7656,565.5469 42.5625,565.625 42.375,565.8125 C41.9219,566.2969 41.9219,566.2969 41.7344,566.3906 C41.3125,566.6563 40.625,566.7813 39.8594,566.7813 C37.8125,566.7813 36.5156,565.6875 36.5156,563.9844 L36.5156,562.8906 C36.5156,561.1094 37.7656,559.7969 39.5,559.7969 C40.0781,559.7969 40.6875,559.9531 41.1563,560.2031 C41.6406,560.4844 41.8125,560.7031 41.9063,561.1094 C41.9688,561.5156 42,561.6406 42.1406,561.7656 C42.2813,561.9063 42.5156,562.0156 42.7344,562.0156 C43,562.0156 43.2656,561.875 43.4375,561.6563 C43.5469,561.5 43.5781,561.3125 43.5781,560.8906 L43.5781,559.4688 C43.5781,559.0313 43.5625,558.9063 43.4688,558.75 C43.3125,558.4844 43.0313,558.3438 42.7344,558.3438 C42.4375,558.3438 42.2344,558.4375 42.0156,558.75 L41.8438,558.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="185" x="53.5" y="567.8467">BBQBufferQueueProducer</text><line style="stroke:#181818;stroke-width:0.5;" x1="25.5" x2="240.5" y1="579" y2="579"/><line style="stroke:#181818;stroke-width:0.5;" x1="25.5" x2="240.5" y1="587" y2="587"/></g><!--class BufferQueueCore--><g id="elem_BufferQueueCore"><rect fill="#F1F1F1" height="48" id="BufferQueueCore" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="156" x="292" y="655"/><ellipse cx="307" cy="671" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M309.3438,666.6719 C308.4063,666.2344 307.8125,666.0938 306.9375,666.0938 C304.3125,666.0938 302.3125,668.1719 302.3125,670.8906 L302.3125,672.0156 C302.3125,674.5938 304.4219,676.4844 307.3125,676.4844 C308.5313,676.4844 309.6875,676.1875 310.4375,675.6406 C311.0156,675.2344 311.3438,674.7813 311.3438,674.3906 C311.3438,673.9375 310.9531,673.5469 310.4844,673.5469 C310.2656,673.5469 310.0625,673.625 309.875,673.8125 C309.4219,674.2969 309.4219,674.2969 309.2344,674.3906 C308.8125,674.6563 308.125,674.7813 307.3594,674.7813 C305.3125,674.7813 304.0156,673.6875 304.0156,671.9844 L304.0156,670.8906 C304.0156,669.1094 305.2656,667.7969 307,667.7969 C307.5781,667.7969 308.1875,667.9531 308.6563,668.2031 C309.1406,668.4844 309.3125,668.7031 309.4063,669.1094 C309.4688,669.5156 309.5,669.6406 309.6406,669.7656 C309.7813,669.9063 310.0156,670.0156 310.2344,670.0156 C310.5,670.0156 310.7656,669.875 310.9375,669.6563 C311.0469,669.5 311.0781,669.3125 311.0781,668.8906 L311.0781,667.4688 C311.0781,667.0313 311.0625,666.9063 310.9688,666.75 C310.8125,666.4844 310.5313,666.3438 310.2344,666.3438 C309.9375,666.3438 309.7344,666.4375 309.5156,666.75 L309.3438,666.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="124" x="321" y="675.8467">BufferQueueCore</text><line style="stroke:#181818;stroke-width:0.5;" x1="293" x2="447" y1="687" y2="687"/><line style="stroke:#181818;stroke-width:0.5;" x1="293" x2="447" y1="695" y2="695"/></g><!--class BnGraphicBufferProducer--><g id="elem_BnGraphicBufferProducer"><rect fill="#F1F1F1" height="48" id="BnGraphicBufferProducer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="216" x="30" y="331"/><ellipse cx="45" cy="347" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M47.3438,342.6719 C46.4063,342.2344 45.8125,342.0938 44.9375,342.0938 C42.3125,342.0938 40.3125,344.1719 40.3125,346.8906 L40.3125,348.0156 C40.3125,350.5938 42.4219,352.4844 45.3125,352.4844 C46.5313,352.4844 47.6875,352.1875 48.4375,351.6406 C49.0156,351.2344 49.3438,350.7813 49.3438,350.3906 C49.3438,349.9375 48.9531,349.5469 48.4844,349.5469 C48.2656,349.5469 48.0625,349.625 47.875,349.8125 C47.4219,350.2969 47.4219,350.2969 47.2344,350.3906 C46.8125,350.6563 46.125,350.7813 45.3594,350.7813 C43.3125,350.7813 42.0156,349.6875 42.0156,347.9844 L42.0156,346.8906 C42.0156,345.1094 43.2656,343.7969 45,343.7969 C45.5781,343.7969 46.1875,343.9531 46.6563,344.2031 C47.1406,344.4844 47.3125,344.7031 47.4063,345.1094 C47.4688,345.5156 47.5,345.6406 47.6406,345.7656 C47.7813,345.9063 48.0156,346.0156 48.2344,346.0156 C48.5,346.0156 48.7656,345.875 48.9375,345.6563 C49.0469,345.5 49.0781,345.3125 49.0781,344.8906 L49.0781,343.4688 C49.0781,343.0313 49.0625,342.9063 48.9688,342.75 C48.8125,342.4844 48.5313,342.3438 48.2344,342.3438 C47.9375,342.3438 47.7344,342.4375 47.5156,342.75 L47.3438,342.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="184" x="59" y="351.8467">BnGraphicBufferProducer</text><line style="stroke:#181818;stroke-width:0.5;" x1="31" x2="245" y1="363" y2="363"/><line style="stroke:#181818;stroke-width:0.5;" x1="31" x2="245" y1="371" y2="371"/></g><!--class BnInterface--><g id="elem_BnInterface"><rect codeLine="8" fill="#F1F1F1" height="48" id="BnInterface" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="262" x="7" y="223"/><ellipse cx="22" cy="239" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M24.3438,234.6719 C23.4063,234.2344 22.8125,234.0938 21.9375,234.0938 C19.3125,234.0938 17.3125,236.1719 17.3125,238.8906 L17.3125,240.0156 C17.3125,242.5938 19.4219,244.4844 22.3125,244.4844 C23.5313,244.4844 24.6875,244.1875 25.4375,243.6406 C26.0156,243.2344 26.3438,242.7813 26.3438,242.3906 C26.3438,241.9375 25.9531,241.5469 25.4844,241.5469 C25.2656,241.5469 25.0625,241.625 24.875,241.8125 C24.4219,242.2969 24.4219,242.2969 24.2344,242.3906 C23.8125,242.6563 23.125,242.7813 22.3594,242.7813 C20.3125,242.7813 19.0156,241.6875 19.0156,239.9844 L19.0156,238.8906 C19.0156,237.1094 20.2656,235.7969 22,235.7969 C22.5781,235.7969 23.1875,235.9531 23.6563,236.2031 C24.1406,236.4844 24.3125,236.7031 24.4063,237.1094 C24.4688,237.5156 24.5,237.6406 24.6406,237.7656 C24.7813,237.9063 25.0156,238.0156 25.2344,238.0156 C25.5,238.0156 25.7656,237.875 25.9375,237.6563 C26.0469,237.5 26.0781,237.3125 26.0781,236.8906 L26.0781,235.4688 C26.0781,235.0313 26.0625,234.9063 25.9688,234.75 C25.8125,234.4844 25.5313,234.3438 25.2344,234.3438 C24.9375,234.3438 24.7344,234.4375 24.5156,234.75 L24.3438,234.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="85" x="36" y="243.8467">BnInterface</text><rect fill="#FFFFFF" height="15.9688" style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" width="143" x="129" y="220"/><text fill="#000000" font-family="sans-serif" font-size="12" font-style="italic" lengthAdjust="spacing" textLength="141" x="130" y="232.1387">IGraphicBufferProducer</text><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="268" y1="255" y2="255"/><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="268" y1="263" y2="263"/></g><!--class BBinder--><g id="elem_BBinder"><rect fill="#F1F1F1" height="48" id="BBinder" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="89" x="247.5" y="115"/><ellipse cx="262.5" cy="131" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M264.8438,126.6719 C263.9063,126.2344 263.3125,126.0938 262.4375,126.0938 C259.8125,126.0938 257.8125,128.1719 257.8125,130.8906 L257.8125,132.0156 C257.8125,134.5938 259.9219,136.4844 262.8125,136.4844 C264.0313,136.4844 265.1875,136.1875 265.9375,135.6406 C266.5156,135.2344 266.8438,134.7813 266.8438,134.3906 C266.8438,133.9375 266.4531,133.5469 265.9844,133.5469 C265.7656,133.5469 265.5625,133.625 265.375,133.8125 C264.9219,134.2969 264.9219,134.2969 264.7344,134.3906 C264.3125,134.6563 263.625,134.7813 262.8594,134.7813 C260.8125,134.7813 259.5156,133.6875 259.5156,131.9844 L259.5156,130.8906 C259.5156,129.1094 260.7656,127.7969 262.5,127.7969 C263.0781,127.7969 263.6875,127.9531 264.1563,128.2031 C264.6406,128.4844 264.8125,128.7031 264.9063,129.1094 C264.9688,129.5156 265,129.6406 265.1406,129.7656 C265.2813,129.9063 265.5156,130.0156 265.7344,130.0156 C266,130.0156 266.2656,129.875 266.4375,129.6563 C266.5469,129.5 266.5781,129.3125 266.5781,128.8906 L266.5781,127.4688 C266.5781,127.0313 266.5625,126.9063 266.4688,126.75 C266.3125,126.4844 266.0313,126.3438 265.7344,126.3438 C265.4375,126.3438 265.2344,126.4375 265.0156,126.75 L264.8438,126.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="57" x="276.5" y="135.8467">BBinder</text><line style="stroke:#181818;stroke-width:0.5;" x1="248.5" x2="335.5" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="248.5" x2="335.5" y1="155" y2="155"/></g><!--class BnGraphicBufferConsumer--><g id="elem_BnGraphicBufferConsumer"><rect fill="#F1F1F1" height="48" id="BnGraphicBufferConsumer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="224" x="333" y="439"/><ellipse cx="348" cy="455" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M350.3438,450.6719 C349.4063,450.2344 348.8125,450.0938 347.9375,450.0938 C345.3125,450.0938 343.3125,452.1719 343.3125,454.8906 L343.3125,456.0156 C343.3125,458.5938 345.4219,460.4844 348.3125,460.4844 C349.5313,460.4844 350.6875,460.1875 351.4375,459.6406 C352.0156,459.2344 352.3438,458.7813 352.3438,458.3906 C352.3438,457.9375 351.9531,457.5469 351.4844,457.5469 C351.2656,457.5469 351.0625,457.625 350.875,457.8125 C350.4219,458.2969 350.4219,458.2969 350.2344,458.3906 C349.8125,458.6563 349.125,458.7813 348.3594,458.7813 C346.3125,458.7813 345.0156,457.6875 345.0156,455.9844 L345.0156,454.8906 C345.0156,453.1094 346.2656,451.7969 348,451.7969 C348.5781,451.7969 349.1875,451.9531 349.6563,452.2031 C350.1406,452.4844 350.3125,452.7031 350.4063,453.1094 C350.4688,453.5156 350.5,453.6406 350.6406,453.7656 C350.7813,453.9063 351.0156,454.0156 351.2344,454.0156 C351.5,454.0156 351.7656,453.875 351.9375,453.6563 C352.0469,453.5 352.0781,453.3125 352.0781,452.8906 L352.0781,451.4688 C352.0781,451.0313 352.0625,450.9063 351.9688,450.75 C351.8125,450.4844 351.5313,450.3438 351.2344,450.3438 C350.9375,450.3438 350.7344,450.4375 350.5156,450.75 L350.3438,450.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="192" x="362" y="459.8467">BnGraphicBufferConsumer</text><line style="stroke:#181818;stroke-width:0.5;" x1="334" x2="556" y1="471" y2="471"/><line style="stroke:#181818;stroke-width:0.5;" x1="334" x2="556" y1="479" y2="479"/></g><!--class BufferQueueConsumer--><g id="elem_BufferQueueConsumer"><rect fill="#F1F1F1" height="48" id="BufferQueueConsumer" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="194" x="348" y="547"/><ellipse cx="363" cy="563" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M365.3438,558.6719 C364.4063,558.2344 363.8125,558.0938 362.9375,558.0938 C360.3125,558.0938 358.3125,560.1719 358.3125,562.8906 L358.3125,564.0156 C358.3125,566.5938 360.4219,568.4844 363.3125,568.4844 C364.5313,568.4844 365.6875,568.1875 366.4375,567.6406 C367.0156,567.2344 367.3438,566.7813 367.3438,566.3906 C367.3438,565.9375 366.9531,565.5469 366.4844,565.5469 C366.2656,565.5469 366.0625,565.625 365.875,565.8125 C365.4219,566.2969 365.4219,566.2969 365.2344,566.3906 C364.8125,566.6563 364.125,566.7813 363.3594,566.7813 C361.3125,566.7813 360.0156,565.6875 360.0156,563.9844 L360.0156,562.8906 C360.0156,561.1094 361.2656,559.7969 363,559.7969 C363.5781,559.7969 364.1875,559.9531 364.6563,560.2031 C365.1406,560.4844 365.3125,560.7031 365.4063,561.1094 C365.4688,561.5156 365.5,561.6406 365.6406,561.7656 C365.7813,561.9063 366.0156,562.0156 366.2344,562.0156 C366.5,562.0156 366.7656,561.875 366.9375,561.6563 C367.0469,561.5 367.0781,561.3125 367.0781,560.8906 L367.0781,559.4688 C367.0781,559.0313 367.0625,558.9063 366.9688,558.75 C366.8125,558.4844 366.5313,558.3438 366.2344,558.3438 C365.9375,558.3438 365.7344,558.4375 365.5156,558.75 L365.3438,558.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="162" x="377" y="567.8467">BufferQueueConsumer</text><line style="stroke:#181818;stroke-width:0.5;" x1="349" x2="541" y1="579" y2="579"/><line style="stroke:#181818;stroke-width:0.5;" x1="349" x2="541" y1="587" y2="587"/></g><!--class SafeBnInterface--><g id="elem_SafeBnInterface"><rect codeLine="13" fill="#F1F1F1" height="48" id="SafeBnInterface" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="301" x="294.5" y="331"/><ellipse cx="309.5" cy="347" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M311.8438,342.6719 C310.9063,342.2344 310.3125,342.0938 309.4375,342.0938 C306.8125,342.0938 304.8125,344.1719 304.8125,346.8906 L304.8125,348.0156 C304.8125,350.5938 306.9219,352.4844 309.8125,352.4844 C311.0313,352.4844 312.1875,352.1875 312.9375,351.6406 C313.5156,351.2344 313.8438,350.7813 313.8438,350.3906 C313.8438,349.9375 313.4531,349.5469 312.9844,349.5469 C312.7656,349.5469 312.5625,349.625 312.375,349.8125 C311.9219,350.2969 311.9219,350.2969 311.7344,350.3906 C311.3125,350.6563 310.625,350.7813 309.8594,350.7813 C307.8125,350.7813 306.5156,349.6875 306.5156,347.9844 L306.5156,346.8906 C306.5156,345.1094 307.7656,343.7969 309.5,343.7969 C310.0781,343.7969 310.6875,343.9531 311.1563,344.2031 C311.6406,344.4844 311.8125,344.7031 311.9063,345.1094 C311.9688,345.5156 312,345.6406 312.1406,345.7656 C312.2813,345.9063 312.5156,346.0156 312.7344,346.0156 C313,346.0156 313.2656,345.875 313.4375,345.6563 C313.5469,345.5 313.5781,345.3125 313.5781,344.8906 L313.5781,343.4688 C313.5781,343.0313 313.5625,342.9063 313.4688,342.75 C313.3125,342.4844 313.0313,342.3438 312.7344,342.3438 C312.4375,342.3438 312.2344,342.4375 312.0156,342.75 L311.8438,342.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="117" x="323.5" y="351.8467">SafeBnInterface</text><rect fill="#FFFFFF" height="15.9688" style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" width="150" x="448.5" y="328"/><text fill="#000000" font-family="sans-serif" font-size="12" font-style="italic" lengthAdjust="spacing" textLength="148" x="449.5" y="340.1387">IGraphicBufferConsumer</text><line style="stroke:#181818;stroke-width:0.5;" x1="295.5" x2="594.5" y1="363" y2="363"/><line style="stroke:#181818;stroke-width:0.5;" x1="295.5" x2="594.5" y1="371" y2="371"/></g><!--class BnInterface2--><g id="elem_BnInterface2"><rect codeLine="14" fill="#F1F1F1" height="48" id="BnInterface2" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="269" x="310.5" y="223"/><ellipse cx="325.5" cy="239" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M327.8438,234.6719 C326.9063,234.2344 326.3125,234.0938 325.4375,234.0938 C322.8125,234.0938 320.8125,236.1719 320.8125,238.8906 L320.8125,240.0156 C320.8125,242.5938 322.9219,244.4844 325.8125,244.4844 C327.0313,244.4844 328.1875,244.1875 328.9375,243.6406 C329.5156,243.2344 329.8438,242.7813 329.8438,242.3906 C329.8438,241.9375 329.4531,241.5469 328.9844,241.5469 C328.7656,241.5469 328.5625,241.625 328.375,241.8125 C327.9219,242.2969 327.9219,242.2969 327.7344,242.3906 C327.3125,242.6563 326.625,242.7813 325.8594,242.7813 C323.8125,242.7813 322.5156,241.6875 322.5156,239.9844 L322.5156,238.8906 C322.5156,237.1094 323.7656,235.7969 325.5,235.7969 C326.0781,235.7969 326.6875,235.9531 327.1563,236.2031 C327.6406,236.4844 327.8125,236.7031 327.9063,237.1094 C327.9688,237.5156 328,237.6406 328.1406,237.7656 C328.2813,237.9063 328.5156,238.0156 328.7344,238.0156 C329,238.0156 329.2656,237.875 329.4375,237.6563 C329.5469,237.5 329.5781,237.3125 329.5781,236.8906 L329.5781,235.4688 C329.5781,235.0313 329.5625,234.9063 329.4688,234.75 C329.3125,234.4844 329.0313,234.3438 328.7344,234.3438 C328.4375,234.3438 328.2344,234.4375 328.0156,234.75 L327.8438,234.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="85" x="339.5" y="243.8467">BnInterface</text><rect fill="#FFFFFF" height="15.9688" style="stroke:#181818;stroke-width:1.0;stroke-dasharray:2.0,2.0;" width="150" x="432.5" y="220"/><text fill="#000000" font-family="sans-serif" font-size="12" font-style="italic" lengthAdjust="spacing" textLength="148" x="433.5" y="232.1387">IGraphicBufferConsumer</text><line style="stroke:#181818;stroke-width:0.5;" x1="311.5" x2="578.5" y1="255" y2="255"/><line style="stroke:#181818;stroke-width:0.5;" x1="311.5" x2="578.5" y1="263" y2="263"/></g><!--class SurfaceControl--><g id="elem_SurfaceControl"><rect fill="#F1F1F1" height="48" id="SurfaceControl" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="140" x="413" y="7"/><ellipse cx="428" cy="23" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M430.3438,18.6719 C429.4063,18.2344 428.8125,18.0938 427.9375,18.0938 C425.3125,18.0938 423.3125,20.1719 423.3125,22.8906 L423.3125,24.0156 C423.3125,26.5938 425.4219,28.4844 428.3125,28.4844 C429.5313,28.4844 430.6875,28.1875 431.4375,27.6406 C432.0156,27.2344 432.3438,26.7813 432.3438,26.3906 C432.3438,25.9375 431.9531,25.5469 431.4844,25.5469 C431.2656,25.5469 431.0625,25.625 430.875,25.8125 C430.4219,26.2969 430.4219,26.2969 430.2344,26.3906 C429.8125,26.6563 429.125,26.7813 428.3594,26.7813 C426.3125,26.7813 425.0156,25.6875 425.0156,23.9844 L425.0156,22.8906 C425.0156,21.1094 426.2656,19.7969 428,19.7969 C428.5781,19.7969 429.1875,19.9531 429.6563,20.2031 C430.1406,20.4844 430.3125,20.7031 430.4063,21.1094 C430.4688,21.5156 430.5,21.6406 430.6406,21.7656 C430.7813,21.9063 431.0156,22.0156 431.2344,22.0156 C431.5,22.0156 431.7656,21.875 431.9375,21.6563 C432.0469,21.5 432.0781,21.3125 432.0781,20.8906 L432.0781,19.4688 C432.0781,19.0313 432.0625,18.9063 431.9688,18.75 C431.8125,18.4844 431.5313,18.3438 431.2344,18.3438 C430.9375,18.3438 430.7344,18.4375 430.5156,18.75 L430.3438,18.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="108" x="442" y="27.8467">SurfaceControl</text><line style="stroke:#181818;stroke-width:0.5;" x1="414" x2="552" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="414" x2="552" y1="47" y2="47"/></g><!--reverse link BLASTBufferQueue to IGraphicBufferProducer--><g id="link_BLASTBufferQueue_IGraphicBufferProducer"><path codeLine="3" d="M244.1395,61.0394 C213.7165,78.7584 182.21,97.108 151.728,114.862 " fill="none" id="BLASTBufferQueue-backto-IGraphicBufferProducer" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="254.509,55,247.3111,54.5632,244.1395,61.0394,251.3374,61.4762,254.509,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BLASTBufferQueue to IGraphicBufferConsumer--><g id="link_BLASTBufferQueue_IGraphicBufferConsumer"><path codeLine="4" d="M343.8605,61.0394 C374.2835,78.7584 405.79,97.108 436.272,114.862 " fill="none" id="BLASTBufferQueue-backto-IGraphicBufferConsumer" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="333.491,55,336.6626,61.4762,343.8605,61.0394,340.6889,54.5632,333.491,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BufferQueueProducer to BBQBufferQueueProducer--><g id="link_BufferQueueProducer_BBQBufferQueueProducer"><path codeLine="5" d="M136.0668,504.98 C135.2338,522.638 134.937,528.941 134.1,546.678 " fill="none" id="BufferQueueProducer-backto-BBQBufferQueueProducer" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="136.915,487,130.0735,504.6973,142.0601,505.2627,136.915,487" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BufferQueueProducer to BufferQueueCore--><g id="link_BufferQueueProducer_BufferQueueCore"><path codeLine="6" d="M187.4376,493.536 C212.1116,508.981 233.761,524.206 259,547 C296.387,580.766 332.786,627.0409 353.208,654.5937 " fill="none" id="BufferQueueProducer-backto-BufferQueueCore" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="177.266,487.169,180.2294,493.743,187.4376,493.536,184.4741,486.962,177.266,487.169" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BnGraphicBufferProducer to BufferQueueProducer--><g id="link_BnGraphicBufferProducer_BufferQueueProducer"><path codeLine="7" d="M138,397 C138,414.658 138,420.941 138,438.678 " fill="none" id="BnGraphicBufferProducer-backto-BufferQueueProducer" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="138,379,132,397,144,397,138,379" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link IGraphicBufferProducer to BnInterface--><g id="link_IGraphicBufferProducer_BnInterface"><path codeLine="9" d="M121.9298,180.4818 C126.2608,198.1398 127.929,204.941 132.28,222.678 " fill="none" id="IGraphicBufferProducer-backto-BnInterface" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="117.642,163,116.1025,181.9111,127.7571,179.0526,117.642,163" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BBinder to BnInterface--><g id="link_BBinder_BnInterface"><path codeLine="10" d="M243.757,173.2059 C218.015,190.9249 197.409,205.108 171.616,222.862 " fill="none" id="BBinder-backto-BnInterface" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="258.584,163,240.355,168.2635,247.159,178.1482,258.584,163" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BnInterface to BnGraphicBufferProducer--><g id="link_BnInterface_BnGraphicBufferProducer"><path codeLine="11" d="M138,289 C138,306.658 138,312.941 138,330.678 " fill="none" id="BnInterface-backto-BnGraphicBufferProducer" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="138,271,132,289,144,289,138,271" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BnGraphicBufferConsumer to BufferQueueConsumer--><g id="link_BnGraphicBufferConsumer_BufferQueueConsumer"><path codeLine="12" d="M445,505 C445,522.658 445,528.941 445,546.678 " fill="none" id="BnGraphicBufferConsumer-backto-BufferQueueConsumer" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="445,487,439,505,451,505,445,487" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BnInterface2 to SafeBnInterface--><g id="link_BnInterface2_SafeBnInterface"><path codeLine="15" d="M445,289 C445,306.658 445,312.941 445,330.678 " fill="none" id="BnInterface2-backto-SafeBnInterface" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="445,271,439,289,451,289,445,271" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link SafeBnInterface to BnGraphicBufferConsumer--><g id="link_SafeBnInterface_BnGraphicBufferConsumer"><path codeLine="16" d="M445,397 C445,414.658 445,420.941 445,438.678 " fill="none" id="SafeBnInterface-backto-BnGraphicBufferConsumer" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="445,379,439,397,451,397,445,379" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link IGraphicBufferConsumer to BnInterface2--><g id="link_IGraphicBufferConsumer_BnInterface2"><path codeLine="17" d="M464.2206,180.2764 C459.0566,197.9344 457.008,204.941 451.82,222.678 " fill="none" id="IGraphicBufferConsumer-backto-BnInterface2" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="469.273,163,458.4618,178.5922,469.9794,181.9605,469.273,163" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BBinder to BnInterface2--><g id="link_BBinder_BnInterface2"><path codeLine="18" d="M339.9949,173.251 C365.5699,190.97 385.977,205.108 411.602,222.862 " fill="none" id="BBinder-backto-BnInterface2" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="325.199,163,336.5779,178.1829,343.4119,168.319,325.199,163" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BLASTBufferQueue to SurfaceControl--><g id="link_BLASTBufferQueue_SurfaceControl"><path codeLine="20" d="M390.164,31 C401.767,31 401.37,31 412.973,31 " fill="none" id="BLASTBufferQueue-backto-SurfaceControl" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="378.164,31,384.164,35,390.164,31,384.164,27,378.164,31" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link BufferQueueConsumer to BufferQueueCore--><g id="link_BufferQueueConsumer_BufferQueueCore"><path codeLine="21" d="M421.795,604.796 C409.301,622.4544 399.051,636.9408 386.501,654.6784 " fill="none" id="BufferQueueConsumer-backto-BufferQueueCore" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="428.726,595,421.9952,597.5876,421.795,604.796,428.5258,602.2083,428.726,595" style="stroke:#181818;stroke-width:1.0;"/></g><!--SRC=[ZPAn3eCW48Ptde8uDd5nDYR5eI7fK6DVW22a91OQbAqFNrj5r1x63Uw- -xd_3qhtmah61QONoruFnOcJKjYRDOtZmY8L0KgZE_SS078zr_UnLZdX13wbQKpi01jq0-RdSVuUU48ge0VoN_b5lJHM8AB1tIPjG9YthEimqTGdaSFk2aJtbCCy8aJf1co4isz6WjHWQ7ycvZH74pMJ8cx4myL49mVG1BDLDjdIGpRkitaLRUwWqtbnkcTqzMuar0n1tmxNxjST17jhMWHa1lsU3m00]--></g></svg>

<h2 id="附录一：Canvas-Java层与Native源码对应关系"><a href="#附录一：Canvas-Java层与Native源码对应关系" class="headerlink" title="附录一：Canvas Java层与Native源码对应关系"></a>附录一：Canvas Java层与Native源码对应关系</h2><p>本节从源码角度说明了Java层的Canvas与Native层函数之间的对应关系。</p>
<ol>
<li><p><code>android.graphics.Canvas</code>、<code>android.graphics.BaseCanvas</code>、<code>android.graphics.BaseRecordingCanvas</code>这三个类中的jni方法都在<code>frameworks\base\libs\hwui\jni\android_graphics_Canvas.cpp</code>中定义：</p>
 <figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">register_android_graphics_Canvas</span><span class="params">(JNIEnv* env)</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">    ret |= <span class="built_in">RegisterMethodsOrDie</span>(env, <span class="string">&quot;android/graphics/Canvas&quot;</span>, gMethods, <span class="built_in">NELEM</span>(gMethods));</span><br><span class="line">    ret |= <span class="built_in">RegisterMethodsOrDie</span>(env, <span class="string">&quot;android/graphics/BaseCanvas&quot;</span>, gDrawMethods, <span class="built_in">NELEM</span>(gDrawMethods));</span><br><span class="line">    ret |= <span class="built_in">RegisterMethodsOrDie</span>(env, <span class="string">&quot;android/graphics/BaseRecordingCanvas&quot;</span>, gDrawMethods, <span class="built_in">NELEM</span>(gDrawMethods));</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>android.graphics.RecordingCanvas</code>对应的jni方法在<code>frameworks\base\libs\hwui\jni\android_graphics_DisplayListCanvas.cpp</code>中定义;</p>
 <figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> <span class="type">char</span>* <span class="type">const</span> kClassPathName = <span class="string">&quot;android/graphics/RecordingCanvas&quot;</span>;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">register_android_view_DisplayListCanvas</span><span class="params">(JNIEnv* env)</span> </span>&#123;</span><br><span class="line">    jclass runnableClass = <span class="built_in">FindClassOrDie</span>(env, <span class="string">&quot;java/lang/Runnable&quot;</span>);</span><br><span class="line">    gRunnableMethodId = <span class="built_in">GetMethodIDOrDie</span>(env, runnableClass, <span class="string">&quot;run&quot;</span>, <span class="string">&quot;()V&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">RegisterMethodsOrDie</span>(env, kClassPathName, gMethods, <span class="built_in">NELEM</span>(gMethods));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>native Canvas对应的是<code>SkiaRecordingCanvas</code>，创建代码对应：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//android-12.1.0_r27\frameworks\base\graphics\java\android\graphics\RecordingCanvas.java</span></span><br><span class="line">    <span class="keyword">private</span> <span class="title function_">RecordingCanvas</span><span class="params">(<span class="meta">@NonNull</span> RenderNode node, <span class="type">int</span> width, <span class="type">int</span> height)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(nCreateDisplayListCanvas(node.mNativeRenderNode, width, height));</span><br><span class="line">        mDensity = <span class="number">0</span>; <span class="comment">// disable bitmap density scaling</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@CriticalNative</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="type">long</span> <span class="title function_">nCreateDisplayListCanvas</span><span class="params">(<span class="type">long</span> node, <span class="type">int</span> width, <span class="type">int</span> height)</span>;</span><br></pre></td></tr></table></figure>

<p>RecordingCanvas构造函数中调用了native函数nCreateDisplayListCanvas，并最终将结果赋值给了父类<code>Canvas#mNativeCanvasWrapper</code>变量，这个变量存储的就是native层的Canvas对象指针，对应c++层<code>SkiaRecordingCanvas</code>类。<code>nCreateDisplayListCanvas</code>源码实现如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//android\android-12.1.0_r27\frameworks\base\libs\hwui\jni\android_graphics_DisplayListCanvas.cpp</span></span><br><span class="line"><span class="function"><span class="type">static</span> jlong <span class="title">android_view_DisplayListCanvas_createDisplayListCanvas</span><span class="params">(CRITICAL_JNI_PARAMS_COMMA jlong renderNodePtr,</span></span></span><br><span class="line"><span class="params"><span class="function">        jint width, jint height)</span> </span>&#123;</span><br><span class="line">    RenderNode* renderNode = <span class="built_in">reinterpret_cast</span>&lt;RenderNode*&gt;(renderNodePtr);</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">reinterpret_cast</span>&lt;jlong&gt;(Canvas::<span class="built_in">create_recording_canvas</span>(width, height, renderNode));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//android\android-12.1.0_r27\frameworks\base\libs\hwui\hwui\Canvas.cpp</span></span><br><span class="line"><span class="function">Canvas* <span class="title">Canvas::create_recording_canvas</span><span class="params">(<span class="type">int</span> width, <span class="type">int</span> height, uirenderer::RenderNode* renderNode)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> uirenderer::skiapipeline::<span class="built_in">SkiaRecordingCanvas</span>(renderNode, width, height);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="示例：drawCircle"><a href="#示例：drawCircle" class="headerlink" title="示例：drawCircle"></a>示例：drawCircle</h3><p><strong>Java层drawCircle</strong></p>
<p>比如我们在View的onDraw函数中画一个圆，代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">onDraw</span><span class="params">(Canvas canvas)</span> &#123;</span><br><span class="line">   mPaint.setColor(getResources().getColor(R.color.red));</span><br><span class="line">   canvas.drawCircle(<span class="number">50f</span>, <span class="number">50f</span>, <span class="number">10f</span>, mPaint); <span class="comment">//void drawCircle(float cx, float cy, float radius, @NonNull Paint paint)</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>onDraw函数的canvas参数实际上是<code>RecordingCanvas</code>类。因为RecordingCanvas没有重写这个drawCircle函数，所以调用的父类<code>BaseRecordingCanvas</code>的drawCircle函数及其对应的jni函数<code>nDrawCircle</code>：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//frameworks\base\graphics\java\android\graphics\BaseRecordingCanvas.java</span></span><br><span class="line"><span class="meta">@FastNative</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title function_">nDrawCircle</span><span class="params">(<span class="type">long</span> nativeCanvas, <span class="type">float</span> cx, <span class="type">float</span> cy, <span class="type">float</span> radius, <span class="type">long</span> nativePaint)</span>;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title function_">drawCircle</span><span class="params">(<span class="type">float</span> cx, <span class="type">float</span> cy, <span class="type">float</span> radius, <span class="meta">@NonNull</span> Paint paint)</span> &#123;</span><br><span class="line">    nDrawCircle(mNativeCanvasWrapper, cx, cy, radius, paint.getNativeInstance());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>Native层drawCircle</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//frameworks\base\libs\hwui\jni\android_graphics_Canvas.cpp</span></span><br><span class="line"><span class="function"><span class="type">static</span> Canvas* <span class="title">get_canvas</span><span class="params">(jlong canvasHandle)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">reinterpret_cast</span>&lt;Canvas*&gt;(canvasHandle);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">static</span> <span class="type">void</span> <span class="title">drawCircle</span><span class="params">(JNIEnv* env, jobject, jlong canvasHandle, jfloat cx, jfloat cy,</span></span></span><br><span class="line"><span class="params"><span class="function">                       jfloat radius, jlong paintHandle)</span> </span>&#123;</span><br><span class="line">    <span class="type">const</span> Paint* paint = <span class="built_in">reinterpret_cast</span>&lt;Paint*&gt;(paintHandle);</span><br><span class="line">    <span class="built_in">get_canvas</span>(canvasHandle)-&gt;<span class="built_in">drawCircle</span>(cx, cy, radius, *paint);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>canvasHandle就是mNativeCanvasWrapper，对应C++的<code>SkiaRecordingCanvas</code>类。SkiaRecordingCanvas中并没有复写drawCirle(其实也有一个drawCircle但是参数签名不匹配)，所以调用父类SkiaCanvas的drawCircle函数。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">SkiaCanvas::drawCircle</span><span class="params">(<span class="type">float</span> x, <span class="type">float</span> y, <span class="type">float</span> radius, <span class="type">const</span> Paint&amp; paint)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">CC_UNLIKELY</span>(radius &lt;= <span class="number">0</span> || paint.<span class="built_in">nothingToDraw</span>())) <span class="keyword">return</span>;</span><br><span class="line">    <span class="built_in">applyLooper</span>(&amp;paint, [&amp;](<span class="type">const</span> SkPaint&amp; p) &#123; mCanvas-&gt;<span class="built_in">drawCircle</span>(x, y, radius, p); &#125;); <span class="comment">//调用mCanvas-&gt;drawCircle</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>SkCanvas* SkiaCanvas::mCanvas</code>成员变量是在子类SkiaRecordingCanvas初始化过程中通过调用<code>SkiaCanvas::reset(&amp;mRecorder);</code>来设置，mCanvas是<code>RecordingCanvas</code>。也就是说RecordingCanvas是SkCanvas的子类。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">RecordingCanvas</span> <span class="keyword">final</span> : <span class="keyword">public</span> SkCanvasVirtualEnforcer&lt;SkNoDrawCanvas&gt; &#123; ...... &#125;</span><br></pre></td></tr></table></figure>

<p>SkCanvasVirtualEnforcer是一个模板类，在skia库中定义，RecordingCanvas实际上继承自SkNoDrawCanvas，SkNoDrawCanvas以同样的方式继承自SkCanvas。</p>
<p>RecordingCanvas和SkNoDrawCanvas都没有复写SkCanvas::drawCircle</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//external\skia\include\core\SkScalar.h</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">float</span> SkScalar;</span><br><span class="line"><span class="comment">//external\skia\src\core\SkCanvas.cpp</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">SkCanvas::drawCircle</span><span class="params">(SkScalar cx, SkScalar cy, SkScalar radius, <span class="type">const</span> SkPaint&amp; paint)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (radius &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        radius = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    SkRect  r;</span><br><span class="line">    r.<span class="built_in">setLTRB</span>(cx - radius, cy - radius, cx + radius, cy + radius);</span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">drawOval</span>(r, paint);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">SkCanvas::drawOval</span><span class="params">(<span class="type">const</span> SkRect&amp; r, <span class="type">const</span> SkPaint&amp; paint)</span> </span>&#123;</span><br><span class="line">    <span class="built_in">TRACE_EVENT0</span>(<span class="string">&quot;skia&quot;</span>, TRACE_FUNC);</span><br><span class="line">    <span class="comment">// To avoid redundant logic in our culling code and various backends, we always sort rects</span></span><br><span class="line">    <span class="comment">// before passing them along.</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">onDrawOval</span>(r.<span class="built_in">makeSorted</span>(), paint); <span class="comment">//最终调用的是子类的onDrawOval函数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//frameworks\base\libs\hwui\RecordingCanvas.h</span></span><br><span class="line">DisplayListData* RecordingCanvas::fDL;</span><br><span class="line"><span class="comment">//F:\github\android\android-12.1.0_r27\frameworks\base\libs\hwui\RecordingCanvas.cpp</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">RecordingCanvas::onDrawOval</span><span class="params">(<span class="type">const</span> SkRect&amp; oval, <span class="type">const</span> SkPaint&amp; paint)</span> </span>&#123;</span><br><span class="line">    fDL-&gt;<span class="built_in">drawOval</span>(oval, paint);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">DisplayListData::drawOval</span><span class="params">(<span class="type">const</span> SkRect&amp; oval, <span class="type">const</span> SkPaint&amp; paint)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">push</span>&lt;DrawOval&gt;(<span class="number">0</span>, oval, paint);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">DrawOval</span> <span class="keyword">final</span> : Op &#123;</span><br><span class="line">    <span class="type">static</span> <span class="type">const</span> <span class="keyword">auto</span> kType = Type::DrawOval;</span><br><span class="line">    <span class="built_in">DrawOval</span>(<span class="type">const</span> SkRect&amp; oval, <span class="type">const</span> SkPaint&amp; paint) : <span class="built_in">oval</span>(oval), <span class="built_in">paint</span>(paint) &#123;&#125;</span><br><span class="line">    SkRect oval;</span><br><span class="line">    SkPaint paint;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">draw</span><span class="params">(SkCanvas* c, <span class="type">const</span> SkMatrix&amp;)</span> <span class="type">const</span> </span>&#123; c-&gt;<span class="built_in">drawOval</span>(oval, paint); &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="待办"><a href="#待办" class="headerlink" title="待办"></a>待办</h3><ul>
<li><input disabled="" type="checkbox"> <code>View#buildLayer</code>的作用是什么？</li>
<li><input disabled="" type="checkbox"> RenderThread会使用surface.lockHardwareCanvas()吗？</li>
</ul>
<h2 id="SurfaceView"><a href="#SurfaceView" class="headerlink" title="SurfaceView"></a>SurfaceView</h2><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://source.android.google.cn/docs/core/graphics/arch-sv-glsv?hl=zh-cn#surfaceview">SurfaceView</a>是一个组件，可用于在 View 层次结构中嵌入其他合成层。使用 SurfaceView 进行渲染时，SurfaceFlinger 会直接将缓冲区合成到屏幕上。如果没有 SurfaceView，您需要将缓冲区合成到屏幕外的 Surface，然后该 Surface 会合成到屏幕上，而使用 SurfaceView 进行渲染可以省去额外的工作。</p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><p><a target="_blank" rel="noopener external nofollow noreferrer" href="https://ljd1996.github.io/2020/11/09/Android-Surface%E5%8E%9F%E7%90%86%E8%A7%A3%E6%9E%90/">Android-Surface原理解析</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://juejin.cn/post/6897029276752625671"><strong>Android-Surface之双缓冲及SurfaceView解析</strong></a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/xx326664162/article/details/108981453">Android 图形架构 之三—— 创建Layer、Surface、SurfaceControl</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://zhuanlan.zhihu.com/p/78125846">Android图形显示系统（一）硬件合成HWC2</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/tyyj90/article/details/107134945">Android 源码 图形系统之请求布局</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/tyyj90/article/details/107989608">Android 源码 图形系统之 relayoutWindow</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.jianshu.com/p/50a30fa6952e">BlastBufferQueue 机制介绍</a><br><a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/suyimin2010/article/details/94970059">onMeasure() 方法多次调用的原因</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/68833f9e.html" rel="prev" title="Bitmap内存分配以及回收">
                  <i class="fa fa-angle-left"></i> Bitmap内存分配以及回收
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/93b40782.html" rel="next" title="Activity启动流程 (Android13)">
                  Activity启动流程 (Android13) <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Jason</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener external nofollow noreferrer" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener external nofollow noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.umd.js" integrity="sha256-oyhjPiYRWGXaAt+ny/mTMWOnN1GBoZDUQnzzgC7FRI4=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>


  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.5.0/mermaid.min.js","integrity":"sha256-K7oJiQlDulzl24ZUFOywuYme1JqBBvQzK6m8qHjt9Gk="}}</script>
  <script type="module" src="/js/zenuml-definition-074a43fa.js"></script>
  <script type="module" src="/js/mermaid-zenuml.esm.min.mjs"></script>
  <script src="/js/third-party/tags/mermaid.js"></script>


  <script src="/js/third-party/fancybox.js"></script>



  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
